题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5795
A Simple Nim
Memory Limit: 65536/65536 K (Java/Others)
题解
打表找规律,发现只有%8的位置的sg值与之前的一个数交换了,其它的sg值都等于它本身。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
const int maxn = 111;
int SG[maxn],vis[maxn+10];
void get_SG() {
SG[0] = 0;
SG[1] = 1;
SG[2] = 2;
for (int i = 3; i < maxn; i++) {
memset(vis, 0, sizeof(vis));
for (int j = 0; j < i; j++) {
vis[SG[j]] = 1;
}
for (int a = 1; a < i; a++) {
for (int b = a; a + b < i; b++) {
int c = i - a - b;
vis[SG[a] ^ SG[b] ^ SG[c]] = 1;
}
}
for (int j = 0;; j++) if (vis[j] == 0) {
SG[i] = j; break;
}
/*printf("%d
", SG[i]);*/
if (SG[i] != i) printf("%d:%d
", i,SG[i]);
}
}
int main() {
//get_SG();
int tc,n;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
int ans = 0;
for (int i = 0; i < n; i++) {
int x; scanf("%d", &x);
if (x % 8==7) ans^=(x+1);
else if (x % 8 == 0) ans^=(x-1);
else ans ^= x;
}
if (!ans) puts("Second player wins.");
else puts("First player wins.");
}
return 0;
}