📌  相关文章
📜  计算由不超过 2^K 的元素组成的 N 长度数组 - 1 个具有最大总和且按位与等于 0

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

计算由不超过 2^K 的元素组成的 N 长度数组 - 1 个具有最大总和且按位与等于 0

给定两个整数NK,任务是找到满足以下条件的N长度数组的数量:

  • 数组元素的总和是最大可能的。
  • 对于i ( 1 ≤ i ≤ N ) 的每个可能值,第i元素应介于02 K – 1之间。
  • 此外,所有数组元素的按位与应为 0。

注意:因为答案可能很大,所以打印答案模 10^9 + 7

例子 :

方法:我们的想法是观察如果数组中所有元素的所有位都是1 ,那么所有元素的按位与不会为0 ,尽管总和会最大化。因此,对于每一位,在至少一个元素中的每一位将1翻转为0 ,以使按位与等于0 ,同时保持总和最大。因此,对于每一位,只需选择一个元素并在那里翻转该位。由于有K位和N个元素,因此答案就是N^K 。请按照以下步骤解决问题:

  • 定义一个函数power(long long x, long long y, int p)并执行以下任务:
    • 将变量res初始化为1以存储结果。
    • 将 x 的值更新为x %p。
    • 如果x等于0,则返回0。
    • 在while循环中迭代直到y大于0并执行以下任务。
      • 如果y为奇数,则将res的值设置为(res*x)%p。
      • y除以2。
      • x的值设置为(x*x)%p。
  • 将变量mod初始化为1e9+7。
  • 将变量ans初始化为函数power(N, K, mod) 返回的值。
  • 执行上述步骤后,打印ans的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate the power of n^k % p
int power(long long x, unsigned int y, int p)
{
    int res = 1;
 
    // Update x if it is more
    // than or equal to p
    x = x % p;
 
    // In case x is divisible by p;
    if (x == 0)
        return 0;
 
    while (y > 0) {
 
        // If y is odd, multiply
        // x with result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Function to count the number of
// arrays satisfying required conditions
int countArrays(int n, int k)
{
    int mod = 1000000007;
    // Calculating N^K
    int ans = power(n, k, mod);
    return ans;
}
 
// Driver Code
int main()
{
    int n = 3, k = 5;
 
    int ans = countArrays(n, k);
    cout << ans << endl;
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to calculate the power of n^k % p
static int power(int x, int y, int p)
{
    int res = 1;
 
    // Update x if it is more
    // than or equal to p
    x = x % p;
 
    // In case x is divisible by p;
    if (x == 0)
        return 0;
 
    while (y > 0)
    {
         
        // If y is odd, multiply
        // x with result
        if ((y & 1) == 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Function to count the number of
// arrays satisfying required conditions
static int countArrays(int n, int k)
{
    int mod = 1000000007;
     
    // Calculating N^K
    int ans = power(n, k, mod);
    return ans;
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 3, k = 5;
    int ans = countArrays(n, k);
     
    System.out.println(ans);
}
}
 
// This code is contributed by shubhamsingh10


Python3
# Python3 program for the above approach
 
# Function to calculate the power of n^k % p
def power(x, y, p):
    res = 1
 
    # Update x if it is more
    # than or equal to p
    x = x % p
 
    # In case x is divisible by p;
    if (x == 0):
        return 0
    while (y > 0):
 
        # If y is odd, multiply
        # x with result
        if (y & 1):
            res = (res * x) % p
 
        # y must be even now
        y = y >> 1  # y = y/2
        x = (x * x) % p
    return res
 
# Function to count the number of
# arrays satisfying required conditions
def countArrays(n, k):
    mod = 1000000007
     
    # Calculating N^K
    ans = power(n, k, mod)
    return ans
 
# Driver Code
n = 3
k = 5
 
ans = countArrays(n, k)
print(ans)
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate the power of n^k % p
static int power(int x, int y, int p)
{
    int res = 1;
 
    // Update x if it is more
    // than or equal to p
    x = x % p;
 
    // In case x is divisible by p;
    if (x == 0)
        return 0;
 
    while (y > 0) {
 
        // If y is odd, multiply
        // x with result
        if ((y & 1) !=0)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Function to count the number of
// arrays satisfying required conditions
static int countArrays(int n, int k)
{
    int mod = 1000000007;
    // Calculating N^K
    int ans = power(n, k, mod);
    return ans;
}
 
// Driver Code
public static void Main()
{
    int n = 3, k = 5;
    int ans = countArrays(n, k);
    Console.Write(ans);
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
243

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