给定一个数组,我们需要计算给定数组的所有可能子集的按位与之和。
例子:
Input : 1 2 3
Output : 9
For [1, 2, 3], all possible subsets are {1},
{2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
Bitwise AND of these subsets are, 1 + 2 +
3 + 0 + 1 + 2 + 0 = 9.
So, the answer would be 9.
Input : 1 2 3 4
Output : 13
请参阅此帖子以获取计数设置位
天真的方法,我们可以使用幂集生成所有子集,然后计算所有子集的按位与。
在一种更好的方法中,我们试图计算哪个数组元素负责将总和生成一个子集。
让我们从最低有效位开始。为了消除其他位的贡献,我们为集合中的所有数字计算数字AND位。包含0的任何此子集将不做任何贡献。所有仅包含1的非空子集将贡献1。总共将有2 ^ n – 1个此类子集,每个子集贡献1。其他方面也是如此。我们得到[0,2,2],3个子集,每个子集给出2。总计3 * 1 + 3 * 2 = 9
Array = {1, 2, 3}
Binary representation
positions 2 1 0
1 0 0 1
2 0 1 0
3 0 1 1
[ 0 2 2 ]
Count set bit for each position
[ 0 3 3 ] subset produced by each
position 2^n -1 i.e. n is total sum
for each position [ 0, 3*2^1, 3*2^0 ]
Now calculate the sum by multiplying
the position value i.e 2^0, 2^1 ... .
0 + 6 + 3 = 9
CPP
// C++ program to calculate sum of Bit-wise
// and sum of all subsets of an array
#include
using namespace std;
#define BITS 32
int andSum(int arr[], int n)
{
int ans = 0;
// assuming representation of each element is
// in 32 bit
for (int i = 0; i < BITS; i++) {
int countSetBits = 0;
// iterating array element
for (int j = 0; j < n; j++) {
// Counting the set bit of array in
// ith position
if (arr[j] & (1 << i))
countSetBits++;
}
// counting subset which produce sum when
// particular bit position is set.
int subset = (1 << countSetBits) - 1;
// multiplying every position subset with 2^i
// to count the sum.
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
// Drivers code
int main()
{
int arr[] = { 1, 2, 3};
int size = sizeof(arr) / sizeof(arr[0]);
cout << andSum(arr, size);
return 0;
}
Java
// Java program to calculate sum of Bit-wise
// and sum of all subsets of an array
class GFG {
static final int BITS = 32;
static int andSum(int arr[], int n)
{
int ans = 0;
// assuming representation of each
// element is in 32 bit
for (int i = 0; i < BITS; i++) {
int countSetBits = 0;
// iterating array element
for (int j = 0; j < n; j++) {
// Counting the set bit of
// array in ith position
if ((arr[j] & (1 << i)) != 0)
countSetBits++;
}
// counting subset which produce
// sum when particular bit
// position is set.
int subset = (1 << countSetBits) - 1;
// multiplying every position
// subset with 2^i to count the
// sum.
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
// Drivers code
public static void main(String args[])
{
int arr[] = { 1, 2, 3};
int size = 3;
System.out.println (andSum(arr, size));
}
}
// This code is contributed by Arnab Kundu.
Python3
# Python3 program to calculate sum of
# Bit-wise and sum of all subsets of
# an array
BITS = 32;
def andSum(arr, n):
ans = 0
# assuming representation
# of each element is
# in 32 bit
for i in range(0, BITS):
countSetBits = 0
# iterating array element
for j in range(0, n) :
# Counting the set bit
# of array in ith
# position
if (arr[j] & (1 << i)) :
countSetBits = (countSetBits
+ 1)
# counting subset which
# produce sum when
# particular bit position
# is set.
subset = ((1 << countSetBits)
- 1)
# multiplying every position
# subset with 2^i to count
# the sum.
subset = (subset * (1 << i))
ans = ans + subset
return ans
# Driver code
arr = [1, 2, 3]
size = len(arr)
print (andSum(arr, size))
# This code is contributed by
# Manish Shaw (manishshaw1)
C#
// C# program to calculate sum of Bit-wise
// and sum of all subsets of an array
using System;
class GFG {
static int BITS = 32;
static int andSum(int[] arr, int n)
{
int ans = 0;
// assuming representation of each
// element is in 32 bit
for (int i = 0; i < BITS; i++) {
int countSetBits = 0;
// iterating array element
for (int j = 0; j < n; j++) {
// Counting the set bit of
// array in ith position
if ((arr[j] & (1 << i)) != 0)
countSetBits++;
}
// counting subset which produce
// sum when particular bit position
// is set.
int subset = (1 << countSetBits) - 1;
// multiplying every position subset
// with 2^i to count the sum.
subset = (subset * (1 << i));
ans += subset;
}
return ans;
}
// Drivers code
static public void Main()
{
int []arr = { 1, 2, 3};
int size = 3;
Console.WriteLine (andSum(arr, size));
}
}
// This code is contributed by Arnab Kundu.
PHP
Javascript
输出:
9