1. 程式人生 > >hdu3032 Nim or not Nim? SG打表找規律

hdu3032 Nim or not Nim? SG打表找規律

      N堆石子,每次兩種操作,1:選取一堆,取走若干個;2:選取一堆,將其分成兩堆石子。取走最後一個石子的輸。

      資料給的很大,一組一組的去求SG函式顯然會超=..算一下較小的SG值,發現0--8的SG值分別為0,1,2,4,3,5,6,8,7....發現還是有規律的,那麼直接按規律算出SG值最後異或一下答案就出來了..

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int f(int x)
{
    int t=x%4;
    if (t==0)
    {
        if (x==0) return 0;
        else return x-1;
    }
    if (t==1 || t==2) return x;
    return x+1;
}
int n,m,p,q;
int main()
{
//    freopen("in.txt","r",stdin);
    int tt;
    scanf("%d",&tt);
    while (tt--)
    {
        scanf("%d",&n);
        int ans=0;
        for (int i=1; i<=n; i++)
        {
            scanf("%d",&m);
            ans=ans^f(m);
        }
        if (ans) puts("Alice");
        else puts("Bob");
    }
    return 0;
}