Happy Matt Friends
Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others)
Total Submission(s): 4907 Accepted Submission(s): 1859
Problem Description
Matt has N friends. They are playing a game together.
Each of Matt’s friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends’magic numbers is no less than M , Matt wins.
Matt wants to know the number of ways to win.
Input
The first line contains only one integer T , which indicates the number of test cases.
For each test case, the first line contains two integers N, M (1 ≤ N ≤ 40, 0 ≤ M ≤ 106).
In the second line, there are N integers ki (0 ≤ ki ≤ 106), indicating the i-th friend’s magic number.
Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y indicates the number of ways where Matt can win.
Sample Input
2
3 2
1 2 3
3 3
1 2 3
Sample Output
Case #1: 4
Case #2: 2
Hint
In the first sample, Matt can win by selecting:
friend with number 1 and friend with number 2. The xor sum is 3.
friend with number 1 and friend with number 3. The xor sum is 2.
friend with number 2. The xor sum is 2.
friend with number 3. The xor sum is 3. Hence, the answer is 4.
题意
给你n个数和一个m,求从n个数中取任意个数异或后的值大于等于m的方案数
思路
这个问题主要求得是n个元素异或大于等于m的组合方案
对于每个
a
i
a_{i}
ai我们可以取或者不取只有两种方案
所以我们得到多项式,
(
x
0
+
x
a
1
)
(
x
0
+
x
a
2
)
(
x
0
+
x
a
3
)
⋯
(
x
0
+
x
a
n
)
(x^{0}+x^{a_{1}})(x^{0}+x^{a_{2}})(x^{0}+x^{a_{3}})\cdots (x^{0}+x^{a_{n}})
(x0+xa1)(x0+xa2)(x0+xa3)⋯(x0+xan)
但是这里的要求是异或操作,与原来的c2[j+k]+=c1[j]
有些不同,原来是的这个操作是母函数中多项式乘法中的系数的分配操作,系数也其实可以看作方案数
那么c2[j^(k*a[i])]+=c1[j]
就是我是否取a[i]的操作的得到的方案数,k=0或1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const maxn = (1 << 20);
long long c1[maxn];
long long c2[maxn];
int a[45];
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
c1[0]=1;
for(int i=1; i<=n; i++)
{
for(int j=0; j<maxn; j++)
for(int k=0; k<2; k++)
c2[j^(k*a[i])]+=c1[j];
memcpy(c1,c2,sizeof(c2));
memset(c2,0,sizeof(c2));
}
long long ans=0;
printf("Case #%d: ",cas++);
for(int i=m; i<maxn; i++)
ans+=c1[i];
printf("%lld\n",ans);
}
return 0;
}