📌  相关文章
📜  大小为 N 的数组计数,其中元素在 [0, (2^K)-1] 范围内具有最大总和 & 按位与 0

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

大小为 N 的数组计数,其中元素在 [0, (2^K)-1] 范围内具有最大总和 & 按位与 0

给定两个整数NK ,任务是找到所有可能的大小为N的数组的计数,其中所有元素的最大和按位与为 0。此外,元素应在02 K -1的范围内。

例子:

方法:为了更好地理解该方法,请参阅以下步骤:

  • 由于最大可能元素为(2 K – 1)且数组的大小为N ,因此如果数组的所有元素都等于最大元素,则总和将最大,即N * (2 K – 1) = N * ( 2 0 + 2 1 + …………….. + 2 K – 1 ) 。请记住,( 2 K – 1) 中有 K 位,并且所有位都已设置。
  • 所以现在要使所有元素的按位与等于 0,我们必须至少在一个元素中取消设置每个位。此外,我们不能在超过 1 个元素中取消设置相同的位,因为在这种情况下sum不会是最大值
  • 取消设置一个元素中的每个位后,最大可能 Sum = N * ( 2 0 + 2 1 + ……… + 2 K – 1 ) – ( 2 0 + 2 1 + ………. + 2 K – 1 ) = (N * 2 K -1 ) – 2 K – 1 = (N – 1) * (2 K – 1)
  • 现在的目标是找到所有方法,通过这些方法我们可以取消设置至少一个元素中的所有K 位。您可以看到,对于取消设置单个位,您有 N 个选项,即您可以在 N 个元素中的任何一个中取消设置它。因此,取消设置 K 位的总方法将是 N K 。这是我们的最终答案。

插图:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Iterative Function to calculate
// (x^y) in O(log y)
int power(int x, int y)
{
 
    // Initialize answer
    int res = 1;
 
    // Check till the number becomes zero
    while (y) {
 
        // If y is odd, multiply x with result
        if (y % 2 == 1)
            res = (res * x);
 
        // y = y/2
        y = y >> 1;
 
        // Change x to x^2
        x = (x * x);
    }
    return res;
}
 
// Driver Code
int main()
{
    int N = 3, K = 2;
    cout << power(N, K);
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
public class GFG
{
 
  // Iterative Function to calculate
  // (x^y) in O(log y)
  static int power(int x, int y)
  {
 
    // Initialize answer
    int res = 1;
 
    // Check till the number becomes zero
    while (y > 0) {
 
      // If y is odd, multiply x with result
      if (y % 2 == 1)
        res = (res * x);
 
      // y = y/2
      y = y >> 1;
 
      // Change x to x^2
      x = (x * x);
    }
    return res;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 3, K = 2;
    System.out.print(power(N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# python3 program for the above approach
 
# Iterative Function to calculate
# (x^y) in O(log y)
def power(x, y):
 
    # Initialize answer
    res = 1
 
    # Check till the number becomes zero
    while (y):
 
        # If y is odd, multiply x with result
        if (y % 2 == 1):
            res = (res * x)
 
        # y = y/2
        y = y >> 1
 
        # Change x to x^2
        x = (x * x)
 
    return res
 
# Driver Code
if __name__ == "__main__":
 
    N = 3
    K = 2
    print(power(N, K))
 
    # This code is contributed by rakeshsahni


C#
// C# code to implement above approach
using System;
class GFG
{
   
  // Iterative Function to calculate
  // (x^y) in O(log y)
  static int power(int x, int y)
  {
 
    // Initialize answer
    int res = 1;
 
    // Check till the number becomes zero
    while (y > 0) {
 
      // If y is odd, multiply x with result
      if (y % 2 == 1)
        res = (res * x);
 
      // y = y/2
      y = y >> 1;
 
      // Change x to x^2
      x = (x * x);
    }
    return res;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 3, K = 2;
    Console.Write(power(N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
9

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