HDU - 5119
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.
InputThe first line contains only one integer T , which indicates the number of test cases.
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.
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. OutputFor 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 3Sample 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.
可以注意到异或值保持在一定范围内,又xor的性质,a^b^b = a
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int T;
int n,m;
int k[45];
ll dp[2][1<<21];
vector<ll> aa;
int main()
{
// freopen("data.txt","r",stdin);
scanf("%d",&T);
int re = 0;
for(int cas = 1 ; cas <= T ; cas++)
{
memset(dp,0,sizeof(dp));
ll re = 0;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&k[i]);
if(m==0)
{
printf("Case #%d: %lld\n",cas,1LL<<n);
continue;
}
dp[0][0] = 1 ;
for(int i=1;i<=n;i++)
{
for(int j=0;j<(1<<21);j++)
{
dp[i%2][j] = dp[(i-1)%2][j] + dp[(i-1)%2][ j^k[i] ];
}
}
for(int i=m;i<(1<<21);i++)
{
re += dp[n%2][i];
}
printf("Case #%d: %lld\n",cas,re);
}
return 0;
}