HDU 3032 Nim or not Nim? (sg函数)

题意:

给你n堆石子,每次操作可以在任意一堆石子里取走一部分或者将这堆石子分成两堆,最后操作的人取胜。


解题思路:

利用sg定理来解组合游戏的和,每堆石子看成一个游戏,对于有x个石子的堆,它的所有后继状态有 0, 1, 2, ... , x-1 ,

(1, x-1), (2, x-2), .. (d, x-d),于是可以很容易处理出小数据的sg函数,然后可以发现4k+3的sg值为4k+4,4k+4的sg值为4k+3,其他所有x的sg值都为x,根据sg定理每堆sg值异或一下就可以解决了。


/* **********************************************
Author      : JayYe
Created Time: 2013-10-31 15:59:33
File Name   : JayYe.cpp
*********************************************** */

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

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


原文地址:https://www.cnblogs.com/james1207/p/3400422.html