您的位置:首页 > 其它

HDU1907 John (尼姆博弈变形)

2017-04-28 19:37 344 查看


John

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 4714    Accepted Submission(s): 2725


Problem Description

Little John is playing very funny game with his younger brother. There is one big box filled with M&Ms of different colors. At first John has to eat several M&Ms of the same color. Then his opponent has to make a turn. And so on. Please note that each player
has to eat at least one M&M during his turn. If John (or his brother) will eat the last M&M from the box he will be considered as a looser and he will have to buy a new candy box.

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.

 

Input

The first line of input will contain a single integer T – the number of test cases. Next T pairs of lines will describe tests in a following format. The first line of each test will contain an integer N – the amount of different M&M colors in a box. Next line
will contain N integers Ai, separated by spaces – amount of M&Ms of i-th color.

Constraints:

1 <= T <= 474,

1 <= N <= 47,

1 <= Ai <= 4747

 

Output

Output T lines each of them containing information about game winner. Print “John” if John will win the game or “Brother” in other case.

 

Sample Input

2
3
3 5 1
1
1

 

Sample Output

John
Brother

 

Source

Southeastern Europe 2007

题目让求得是拿走最后一个糖果的人为失败者,具体讲解看下面代码

/*
**题目让求的是最后取光者为失败者,也就是说在取糖果的时候都要躲着最后一个糖果。首先来分析一下各种状态,
**假设S态表示异或和非0,T态表示异或和为0,一堆糖果数量为1的称为孤独堆,否则称为充裕堆;Si或者Ti表示充裕堆的个数
** S0:表示有奇数个孤独堆,则先手必败;
** S1:表示有一个充裕堆,当孤独堆的个数为偶数时,先手只需要使得当前充裕堆只剩下一个即可,否则拿完即可,则先手必胜。
** T1:不存在这种状态,即不存在异或和为0且只有一个充裕堆的状态
** T0:表示有偶数个孤独堆。则先手必胜;
** S2:有两个以及以上的充裕堆,异或和非0;
** T2:有两个以上的充裕堆,异或和为0;
** 现在主要来分析下S2和T2态,从上面可知,S0先手必败,S1,T0先手必胜,又根据 Nim博弈,可以由奇异态转换成非奇异态,
** 所以S2可以转换成T2,T2可以转换成S2或者S1,所以T2先手必败,S2先手必胜。
*/

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

int t,n,a[55];

int main(){
scanf("%d",&t);
while(t --){
scanf("%d",&n);
int res = 0,dou = 0;
for(int i = 0; i < n; i ++){
scanf("%d",&a[i]);
res ^= a[i];
if(a[i] > 1)
dou ++;
}

if((n%2 && !dou) || (!res && dou > 1))//奇数堆孤独堆或者异或和为0且充裕堆大于1
printf("Brother\n");
else
printf("John\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: