Happy Matt Friends
Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others)Total Submission(s): 2966 Accepted Submission(s): 1167
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.
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 ≤ 10 6).
In the second line, there are N integers ki (0 ≤ k i ≤ 10 6), indicating the i-th friend’s magic number.
For each test case, the first line contains two integers N, M (1 ≤ N ≤ 40, 0 ≤ M ≤ 10 6).
In the second line, there are N integers ki (0 ≤ k i ≤ 10 6), 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: 2HintIn 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.
Source
题意:
给你n个数,问这n个数的子集的异或和大于等于m的个数。
题解:
看到异或马上想到的是01字典树。然后穷举子集判断就可以了,不过子集的数量可能太多,需要用到白皮书讲的中途相遇法(见本blog 白皮书例题分类)。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define MAX 1000010
using namespace std;
int t,a,n,m;
int ch[22*MAX][2];
int val[22*MAX];
int num[22*MAX];
int sz;
int b[MAX];
int A[50];
void init(){
mem(ch[0]);
sz=1;
memset(num,0,sizeof(num));
}
void inser(int a){
int u=0;
for(int i=22;i>=0;i--){
int c=((a>>i)&1);
if(!ch[u][c]){
mem(ch[sz]);
val[sz]=0;
num[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
num[u]++;
}
val[u]=a;
}
/*void update(int a,int d){
int u=0;
for(int i=22;i>=0;i--){
int c=((a>>i)&1);
u=ch[u][c];
num[u]+=d;
}
}*/
int query(int a)
{
int u=0;
int ans=0;
for(int i=22; i>=0; i--)
{
int c=((a>>i)&1);
int cc=((m>>i)&1);
if(cc)
{
if(!ch[u][c^1])
return ans;
u=ch[u][c^1];
}
else
{
if(ch[u][c^1])
ans+=num[ch[u][c^1]];
if(!ch[u][c]) return ans;
u=ch[u][c];
}
}
return ans+num[u];
}
int main()
{
int t;
scanf("%d",&t);
int ge=1;
while(t--)
{
init();
int sum;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
int n1=n/2;
int n2=n-n1;
for(int i=0;i<(1<<n1);i++)
{
sum=0;
for(int j=0;j<n1;j++)
{
if(i&(1<<j))
sum^=A[j];
}
inser(sum);
}
long long ans=0;
for(int i=0;i<(1<<n2);i++)
{
sum=0;
for(int j=0;j<n2;j++)
{
if(i&(1<<j))
sum^=A[j+n1];
}
ans+=query(sum);
}
printf("Case #%d: %lld\n",ge++,ans);
}
}