[SHOI2008]小约翰的游戏

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

Sample Output
John
Brother


Anti Nim游戏,详情参见浅谈算法——博弈论中的例9

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
	int x=0,f=1;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x>=10)     print(x/10);
	putchar(x%10+'0');
}
int main(){
	for (int Data=read();Data;Data--){
		int n=read(),ans=0;
		bool flag=0;
		for (int i=1;i<=n;i++){
			int x=read();
			ans^=x,flag|=(x>1);
		}
		printf(flag^(ans>0)?"Brother":"John");
		putchar('\n');
	}
	return 0;
}
posted @ 2018-02-08 22:54  Wolfycz  阅读(417)  评论(0编辑  收藏  举报