题目来源:

http://acm.hdu.edu.cn/showproblem.php?pid=3032

分析:

Lasker's Nim游戏: sg(4k)=4k-1;sg(4k+1)=4k+1;sg(4k+2)=4k+2;sg(4k+3)=4k+4;

打表 代码:

int Get_SG(int x)         
{
        int visited[101]={0};
        if(sg[x] != -1) return sg[x]; //  同一个集合, 如果已经计算过的sg[x], 可以重复利用
        for(int i= x - 1 ; i>= 0  ; i--)
            visited[Get_SG(i)] = 1 ; // 取石子
        for(int j=1; j <= x /2; j++){ // 把一堆石子分两堆
            int ans = 0 ;
            ans ^= Get_SG(j) ;
            ans ^= Get_SG(x - j) ;
            visited[ans] = 1 ;
        }
        for(int i=0;;i++)
            if(!visited[i]) return sg[x]=i;
}

 

找出规律》

 本题代码如下:

int g(int x){
    if(x == 0)
        return 0 ;
    if(x % 4 == 0)
        return x -1 ;
    if( (x % 4 == 1) ||( x % 4 == 2))
        return x;
    if(x % 4 == 3)
        return x + 1 ;
}
int main()
{
    int t, n;
    scanf("%d", &t) ;
    while(t--){
        scanf("%d", &n) ;
        int x , res = 0;
        for(int i=0 ; i< n ;i++){
            scanf("%d" , &x) ;
            res ^=  g(x) ;
        }
        if(res )
            puts("Alice") ;
        else puts("Bob") ;

    }
   return 0 ;
}