HDU 3032 nim or not nim (sg function table)

Question meaning:
There are n piles of stones, at least 1 stone can be taken from one pile of stones at a time, or a pile of stones can be divided into two piles, and ask whether the first player must win or lose.
Solution:
Use the SG function to find the law by typing the table, sg [0]=0; sg[1]=1; When there are 2 stones, they can become 0, 1, or (1, 1) after operation. . . If the sg XOR of the number of stones in all piles is 0, it will lose. Otherwise, it will win.
Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int MAXN=1e6+5;
int vis[1000];
int sg[1000]; 
void getsg()
{
    
    
    sg[0]=0;
    sg[1]=1;
    for(int i=2;i<=100;i++)
    {
    
    
        memset(vis,0,sizeof vis);
        for(int j=1;j<=i;j++)
        {
    
    
            vis[sg[i-j]]=1;
        }
        for(int j=1;j<=i-1;j++)
        {
    
    
            vis[sg[j]^sg[i-j]]=1;
        }
        for(int j=0;;j++)
        {
    
    
            if(vis[j]==0)
            {
    
    
                sg[i]=j;
                break;
            }
        }
    }
}
int main()
{
    
    
    /*getsg();
    for(int i=1;i<=50;i++)
    {
        cout<<i<<" "<<sg[i]<<endl;
    }*/
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n;
        scanf("%d",&n);
        int x;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
    
    
            scanf("%d",&x);
            if(x%4==0) ans^=(x-1);
            else if(x%4==3) ans^=(x+1);
            else ans^=x;
        }
        if(ans==0) printf("Bob\n");
        else printf("Alice\n");
    }
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/108749885