大小为 N 的数组计数,其中元素在 [0, (2^K)-1] 范围内具有最大总和 & 按位与 0
给定两个整数N和K ,任务是找到所有可能的大小为N的数组的计数,其中所有元素的最大和和按位与为 0。此外,元素应在0到2 K -1的范围内。
例子:
Input: N = 3, K = 2
Output: 9
Explanation: 22 – 1 = 3, so elements of arrays should be between 0 to 3. All possible arrays are- [3, 3, 0], [1, 2, 3], [3, 0, 3], [0, 3, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] Bitwise AND of all the arrays is 0 & also the sum = 6 is maximum
Input: N = 2, K = 2
Output: 4
Explanation: All possible arrays are – [3, 0], [0, 3], [1, 2], [2, 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 。这是我们的最终答案。
插图:
Let N = 3, K = 3
- Make all elements of the array equal to 23 – 1 = 7. The array will be [7, 7, 7]. Take binary representation of all elements : [111, 111, 111].
- Unset each bit in exactly one element. Suppose we unset the 3rd bit of the 1st element and the 1st two bits of the 2nd element. array becomes [110, 001, 111] = [6, 1, 7]. This is one of the valid arrays. You can generate all arrays in such a way.
- The total number of arrays will be 33 = 27.
下面是上述方法的实现:
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)