标签:des style blog http color java os io
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 724 Accepted Submission(s): 285
题目大意:求有多少方法拿走一些堆的石头,使得先手必胜。
解题思路:构造m*n的矩阵(最多31行),高斯消元求秩r,2^(m-r)即为答案。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 typedef __int64 LL; 8 const int mod=1000007; 9 int A[105][35],maxm; 10 void swap(int &a,int &b){int t=a;a=b;b=t;} 11 int max(int a,int b){return a>b?a:b;} 12 13 void build_matrix(int n) 14 { 15 memset(A,0,sizeof(A)); 16 maxm=-1; 17 for(int i=0;i<n;i++) 18 { 19 int temp; 20 scanf("%d",&temp); 21 for(int j=0;;j++) 22 { 23 if(!temp) break; 24 A[j][i]=temp%2;temp/=2; 25 maxm=max(maxm,j); 26 } 27 } 28 } 29 30 int gauss(int n,int m) 31 { 32 int i=0,j=0,k,r,u; 33 while(i<n&&j<m) 34 { 35 r=i; 36 for(k=i;k<n;k++) 37 if(A[k][j]){r=k;break;} 38 if(A[r][j]) 39 { 40 if(r!=i) for(k=0;k<=m;k++) swap(A[r][k],A[i][k]); 41 for(u=i+1;u<n;u++) if(A[u][j]) 42 for(k=i;k<=m;k++) A[u][k]^=A[i][k]; 43 i++; 44 } 45 j++; 46 } 47 return i; 48 } 49 50 LL pow_mod(LL a,LL b) 51 { 52 LL ret=1;a%=mod; 53 while(b) 54 { 55 if(b&1) ret=ret*a%mod; 56 a=a*a%mod; 57 b>>=1; 58 } 59 return ret; 60 } 61 int main() 62 { 63 int t,n; 64 scanf("%d",&t); 65 while(t--) 66 { 67 scanf("%d",&n); 68 build_matrix(n); 69 int ans=gauss(maxm+1,n); 70 printf("%d\n",pow_mod(2,n-ans)); 71 } 72 return 0; 73 }
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/xiong-/p/3913730.html