Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others)
Total Submission(s): 1810 Accepted Submission(s): 715
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 ≤ 106).
In the second line, there are N integers ki (0 ≤ ki ≤ 106), indicating the i-th friend’s magic number.
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 const int INF=0x3f3f3f3f; 7 const int MAXN=1<<20; 8 int dp[2][MAXN<<1];//刚开始开到MAXN+10 RE,改成这才对; 9 int main(){ 10 int T,N,M,flot=0; 11 int m[41]; 12 scanf("%d",&T); 13 while(T--){ 14 scanf("%d%d",&N,&M); 15 memset(dp,0,sizeof(dp)); 16 dp[0][0]=1; 17 int x=0; 18 for(int i=0;i<N;i++){ 19 x^=1; 20 scanf("%d",m+i); 21 for(int j=0;j<=MAXN;j++){ 22 dp[x][j]=dp[x^1][j]+dp[x^1][j^m[i]]; 23 //代表上一次值为j^m[i]现在再^m[i]等于j了再加上上次j的个数; 24 } 25 } 26 long long ans=0; 27 for(int i=M;i<=MAXN;i++)ans+=dp[x][i]; 28 printf("Case #%d: %lld\n",++flot,ans); 29 } 30 return 0; 31 }
暴力超时:
1 #include<stdio.h> 2 int ans,dt[41]; 3 int N,M; 4 void dfs(int top,int sox,int num,int t){ 5 if(num==t){ 6 if(sox>=M)ans++; 7 return; 8 } 9 for(int i=top;i<N;i++){ 10 dfs(i+1,sox^dt[i],num+1,t); 11 } 12 } 13 int main(){ 14 int T; 15 scanf("%d",&T); 16 while(T--){ 17 scanf("%d%d",&N,&M); 18 for(int i=0;i<N;i++)scanf("%d",dt+i); 19 ans=0; 20 for(int i=1;i<=N;i++){ 21 dfs(0,0,0,i); 22 } 23 printf("%d\n",ans); 24 } 25 return 0; 26 }