HDU 3915 Game(拆位,高斯消元)

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
 
分析:由nim遊戲規則知:先手輸的條件就是xor值爲0
所以我們要分析亦或值,將N個數的選與不選看成開關,開關影響的狀態爲0~30的每一位的狀態
其實拆位後就是一類開關問題答案是2^(自由變元個數)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1000007;
int t,mp[35][110],n;
int A[110];
int equ,var;
int X[55],free_x[55];
int free_num;
int Gauss()
{
    int max_r,col,k;
    free_num=0;
    for(k=0,col=0;k<equ&&col<var;k++,col++)
    {
        max_r=k;
        for(int i=k+1;i<equ;i++)
        {
            if(abs(mp[i][col])>abs(mp[max_r][col]))
                max_r=i;
        }
        if(mp[max_r][col]==0)
        {
            k--;
            free_x[free_num++]=col;
            continue;
        }
        if(max_r!=k)
        {
            for(int j=col;j<var+1;j++)
                swap(mp[k][j],mp[max_r][j]);
        }
        for(int i=k+1;i<equ;i++)
        {
            if(mp[i][col]!=0)
            {
                for(int j=col;j<var+1;j++)
                    mp[i][j]^=mp[k][j];
            }
        }
    }
    for(int i=k;i<equ;i++)
        if(mp[i][col]!=0)  return -1;
    return var-k;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        CLEAR(mp,0);
        for(int i=0;i<n;i++)
           scanf("%d",&A[i]);
        for(int i=0;i<31;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(A[j]&(1<<i))
                    mp[i][j]=1;
            }
        }
        equ=31;var=n;
        int ans=Gauss();
        if(ans==-1)
        {
            puts("0");
            continue;
        }
        LL sum=1;
        //cout<<"fuck  "<<ans<<endl;
        for(int i=0;i<ans;i++)
            sum=sum*2LL%mod;
        printf("%lld\n",sum);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章