📌  相关文章
📜  元素在 [0, 2^K – 1] 范围内且按位与等于 0 的 N 大小最大和数组的计数

📅  最后修改于: 2022-05-13 01:56:10.746000             🧑  作者: Mango

元素在 [0, 2^K – 1] 范围内且按位与等于 0 的 N 大小最大和数组的计数

给定两个正整数NK ,任务是找到大小为N的数组的数量,使得每个数组元素位于[0, 2 K – 1]范围内,其中数组元素的最大总和具有所有数组的按位与元素0

例子:

方法:可以通过观察以下事实来解决给定的问题:由于生成的数组的按位与应该是0 ,那么对于[0, K – 1]范围内的每个i应该至少有 1 个元素具有第 i在其二进制表示中位等于0 。因此,为了最大化数组的总和,最好有 1 个元素且第i位未设置。

因此,对于K位中的每一个,有N C 1种方法可以使其在 1 个数组元素中取消设置。因此,具有最大和的数组的结果计数由N K给出。

以下是该方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the value of X to
// the power Y
int power(int x, unsigned int y)
{
    // Stores the value of X^Y
    int res = 1;
 
    while (y > 0) {
 
        // If y is odd, multiply x
        // with result
        if (y & 1)
            res = res * x;
 
        // Update the value of y and x
        y = y >> 1;
        x = x * x;
    }
 
    // Return the result
    return res;
}
 
// Function to count number of arrays
// having element over the range
// [0, 2^K - 1] with Bitwise AND value
// 0 having maximum possible sum
void countArrays(int N, int K)
{
    // Print the value of N^K
    cout << int(power(N, K));
}
 
// Driver Code
int main()
{
    int N = 5, K = 6;
    countArrays(N, K);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
 
// Function to find the value of X to
// the power Y
static int power(int x, int y)
{
   
    // Stores the value of X^Y
    int res = 1;
 
    while (y > 0) {
 
        // If y is odd, multiply x
        // with result
        if (y%2!=0)
            res = res * x;
 
        // Update the value of y and x
        y = y >> 1;
        x = x * x;
    }
 
    // Return the result
    return res;
}
 
// Function to count number of arrays
// having element over the range
// [0, 2^K - 1] with Bitwise AND value
// 0 having maximum possible sum
static void countArrays(int N, int K)
{
   
    // Print the value of N^K
   System.out.println((int)(power(N, K)));
}
 
 
// Driver Code
public static void main(String args[])
{
    int N = 5, K = 6;
    countArrays(N, K);
}
}
 
// This code is contributed by SoumikMondal


Python3
# Python Program for the above approach
 
# Function to find the value of X to
# the power Y
def power(x, y):
    # Stores the value of X^Y
    res = 1
 
    while (y > 0):
 
        # If y is odd, multiply x
        # with result
        if (y & 1):
            res = res * x
 
        # Update the value of y and x
        y = y >> 1
        x = x * x
 
    # Return the result
    return res
 
# Function to count number of arrays
# having element over the range
# [0, 2^K - 1] with Bitwise AND value
# 0 having maximum possible sum
def countArrays(N, K):
    # Print the value of N^K
    print(power(N, K))
 
 
# Driver Code
 
N = 5;
K = 6;
countArrays(N, K)
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the value of X to
// the power Y
static int power(int x, int y)
{
     
    // Stores the value of X^Y
    int res = 1;
 
    while (y > 0)
    {
         
        // If y is odd, multiply x
        // with result
        if (y % 2 != 0)
            res = res * x;
 
        // Update the value of y and x
        y = y >> 1;
        x = x * x;
    }
 
    // Return the result
    return res;
}
 
// Function to count number of arrays
// having element over the range
// [0, 2^K - 1] with Bitwise AND value
// 0 having maximum possible sum
static void countArrays(int N, int K)
{
     
    // Print the value of N^K
    Console.WriteLine((int)(power(N, K)));
}
 
// Driver Code
public static void Main()
{
    int N = 5, K = 6;
     
    countArrays(N, K);
}
}
 
// This code is contributed by subhammahato348


Javascript


输出:
15625

时间复杂度: O(log K)
辅助空间: O(1)