您的位置:首页 > 其它

bzoj1022: [SHOI2008]小约翰的游戏John

2017-03-16 09:39 295 查看
题目链接

反Nim游戏。

先上结论:一个状态为必胜态,当且仅当所有堆的石子个数为1,且Nim和为0 或 至少有一堆的石子个数大于1,且Nim和不为0。

证明如下:显然所有堆石子个数都为1且石子对数为偶数时先手必胜,而只有一堆石子个数不为1时,Nim和不为0,可以将该堆拿完或拿到只剩一个石子的状态从而使对手处于必败状态。其余情况下,Nim和为0时只能转移至Nim和不为0且至少有一堆石子个数大于1的情况,从而无法取得游戏的胜利。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 55;

char buf;
inline int read() {
int rst = 0;
do buf = getchar(); while (! isdigit(buf));
while (isdigit(buf)) rst = rst*10 + buf - '0', buf = getchar();
return rst;
}

int T, n, nim, tmp;
bool flag;

inline bool judge() {
while (n--) {
tmp = read();
if (tmp^1) flag = true;
nim ^= tmp;
}
if (flag) return nim;
else return !nim;
}

int main() {
T = read();
while (T--) {
n = read(); nim = 0; flag = false;
if (judge()) puts("John");
else puts("Brother");
} return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: