Both of players are using optimal game strategy. John starts first always. You will be given information about M&Ms and your task is to determine a winner of such a beautiful game.
Constraints:
1 <= T <= 474,
1 <= N <= 47,
1 <= Ai <= 4747
2 3 3 5 1 1 1
nim博弈
对于取光者胜利的规则下 异或=0则先手必胜。
对于取光者失败的规则下 异或=0则先手不一定必败。 如果所有的堆都为1的话,那么就看堆的奇偶数了。
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<cmath>
using namespace std;
int n,a;
int main()
{
int t,u;
cin>>t;
while(t--)
{
cin>>n;
u=n;
int sum=0;
int flag=0;
while(n--)
{
cin>>a;
sum^=a;
if(a>1)
{
flag=1;
}
}
if(flag)
{
if(sum==0)
{
cout<<"Brother"<<endl;
}
else
{
cout<<"John"<<endl;
}
}
else
{
if(u%2)
{
cout<<"Brother"<<endl;
}
else
{
cout<<"John"<<endl;
}
}
}
}