首页 > 代码库 > hdu 3915 高斯消元

hdu 3915 高斯消元

Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 724    Accepted Submission(s): 285


Problem Description
  Mr.Frost is a child who is too simple, sometimes naive, always plays some simple but interesting games with his friends. Today,he invents a new game again:
  At the beginning of the game they pick N (1<=N<=100) piles of stones, Mr.Frost and his friend move the stones in turn. At each step of the game, the player chooses a pile, removes at least one stone from the pile, the first player can’t make a move, and loses. So smart is the friends of Mr.Frost that Mr.Frost always loses. Having been a loser for too many times, he wants to play a trick. His plan is to remove some piles, and then he can find a way to make sure that he would be the winner after his friends remove stones first.

Now, he wants to know how many ways to remove piles which are able to achieve his purpose. If it’s impossible to find any way, please print “-1”.
 

 

Input
The first line contains a single integer t (1<=t<=20), that indicates the number of test cases. Then follow the t cases. Each test case begins with a line contains an integer N (1 <= N <= 100), representing the number of the piles. The next n lines, each of which has a positive integer Ai(1<=Ai<=2^31 - 1) represent the number of stones in this pile.
 

 

Output
  For each case, output a line contains the number of the way mod 1000007, If it’s impossible to find any way, please print “-1”.
 

 

Sample Input
2
2
1
1
3
1
2
3
 

 

Sample Output
2
2
 

 

Source
2011 Multi-University Training Contest 8 - Host by HUST

 

题目大意:求有多少方法拿走一些堆的石头,使得先手必胜。

解题思路:构造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 }