1022: [SHOI2008]小约翰的游戏John
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
这是个nim游戏的变形。。
没细看直接异或就WA了。。。
然后发现原来是取到最后一个算输。。。
显然如果有一堆不是1,那么和nim游戏是一样的。。
因为后手得到两个相同的两堆后最后一次操作可以少取一个,这样就可以获胜。。
而如果都是的话就不能这么做了。。。
所以需要对1进行特判。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 10000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int read(){ 24 int x=0,f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 27 return x*f; 28 } 29 int main(){ 30 //freopen("input.txt","r",stdin); 31 //freopen("output.txt","w",stdout); 32 int n=read(); 33 for1(i,n){ 34 int m=read(); 35 int x=read(),flag=1; 36 if(x!=1)flag=0; 37 for1(i,m-1){ 38 int y=read();if(y!=1)flag=0; 39 x^=y; 40 } 41 if(flag==1){ 42 if(x==0)cout<<"John"<<endl; 43 else cout<<"Brother"<<endl; 44 } 45 else{ 46 if(x==0)cout<<"Brother"<<endl; 47 else cout<<"John"<<endl; 48 } 49 } 50 return 0; 51 }