HDU 5795 博弈

时间:2021-08-07 14:27:06

题目链接:

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

A Simple Nim

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
> Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.
#### 输入
> Intput contains multiple test cases. The first line is an integer 1≤T≤100, the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n−1], representing heaps with s[0],s[1],...,s[n−1] objects respectively.(1≤n≤106,1≤s[i]≤109)
#### 输出
> For each test case,output a line whick contains either"First player wins."or"Second player wins".
#### 样例
> **sample input**
> 2
> 2
> 4 4
> 3
> 1 2 4
>
> **sample output**
> Second player wins.
> First player wins.

题解

打表找规律,发现只有%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\n", SG[i]);*/
if (SG[i] != i) printf("%d:%d\n", 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;
}