這個題一看就是nim游戲的變形。每次先手取出巧克力就是新建一個nim,但假如先手取一個為0的而且無論后手怎么取剩下的都無法為零就行了。然后用dfs跑。
題干:
Description TBL和X用巧克力棒玩游戲。每次一人可以從盒子里取出若干條巧克力棒,或是將一根取出的巧克力棒吃掉正整數長度。TBL先手兩人輪流,無法操作的人輸。 他們以最佳策略一共進行了10輪(每次一盒)。你能預測勝負嗎? Input 輸入數據共20行。 第2i-1行一個正整數Ni,表示第i輪巧克力棒的數目。 第2i行Ni個正整數Li,j,表示第i輪巧克力棒的長度。 Output 輸出數據共10行。 每行輸出“YES”或“NO”,表示TBL是否會贏。如果勝則輸出"NO",否則輸出"YES"
代碼:
#include<iostream> #include<cstdio> #include<cmath> #include<ctime> #include<queue> #include<algorithm> #include<cstring> using namespace std; #define duke(i,a,n) for(int i = a;i <= n;i++) #define lv(i,a,n) for(int i = a;i >= n;i--) #define clean(a) memset(a,0,sizeof(a)) const int INF = 1 << 30; typedef long long ll; typedef double db; template <class T> void read(T &x) { char c; bool op = 0; while(c = getchar(), c < '0' || c > '9') if(c == '-') op = 1; x = c - '0'; while(c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0'; if(op) x = -x; } template <class T> void write(T x) { if(x < 0) putchar('-'), x = -x; if(x >= 10) write(x / 10); putchar('0' + x % 10); } int n,sg[2020],a[2010],found; void dfs(int x,int used,int now) { if(x == n + 1) { if(!now && used > 0) found = 1; return; } dfs(x + 1,used,now); dfs(x + 1,used + 1,now ^ a[x]); } int main() { int k = 10; while(k--) { memset(sg,-1,sizeof(sg)); found = 0; read(n); duke(i,1,n) read(a[i]); dfs(1,0,0); if(found == 1) printf("NO\n"); else printf("YES\n"); } return 0; }