1022: [SHOI2008]小约翰的游戏John
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2299 Solved: 1447
[ Submit][ Status][ Discuss]
Description
小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取
的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取,我们规定取到最后一
粒石子的人算输。小约翰相当固执,他坚持认为先取的人有很大的优势,所以他总是先取石子,而他的哥哥就聪明
多了,他从来没有在游戏中犯过错误。小约翰一怒之前请你来做他的参谋。自然,你应该先写一个程序,预测一下
谁将获得游戏的胜利。
Input
本题的输入由多组数据组成第一行包括一个整数T,表示输入总共有T组数据(T≤500)。每组数据的第一行包
括一个整数N(N≤50),表示共有N堆石子,接下来有N个不超过5000的整数,分别表示每堆石子的数目。
Output
每组数据的输出占一行,每行输出一个单词。如果约翰能赢得比赛,则输出“John”,否则输出“Brother”
,请注意单词的大小写。
Sample Input
2
3
3 5 1
1
1
3
3 5 1
1
1
Sample Output
John
Brother
Brother
HINT
Source
这里有一份讲博弈论很不错的blog传送门
然后这是属于anti-nim博弈
实际上只要掌握了一些状态的结论就行了。。
然后blog里面的证明很详细
先手必胜的是S2,S1,T0而T2,S0后手必胜
S和T是xor和的状态,S代表和不为0而T代表和为0
后面的数字分别代表充裕堆堆数
0是没有,1是1个,2是以上。。(此处blog省略了)
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
int T,n;
int getint()
{
char ch = getchar();
int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
T = getint();
while (T--) {
n = getint();
int s = 0,tot = 0;
while (n--) {
int x = getint();
s ^= x;
if (x > 1) ++tot;
}
if (!s) {
if (!tot) puts("John");
else puts("Brother");
}
else {
if (!tot) puts("Brother");
else puts("John");
}
}
return 0;
}