John

来源:互联网 发布:操作系统fifo算法 编辑:程序博客网 时间:2024/06/10 18:45

John

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 205    Accepted Submission(s): 106

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
233 5 111

Sample Output
JohnBrother
 
Source
Southeastern Europe 2007

这一题有点类似于NIM游戏,当符合一定条件的时候,先手可必胜。这里用到了一个规律。

把每一堆的数目进行异或运算(每一堆的数目都是1除外),最后的结果有两种,为0或不为0,若为0则各堆的各二进制位相加不进位以后所得到的数的各位数一定是一个偶数。我们称结果为0的情况为平衡状态,如果刚开始局面是一个不平衡状态,即各堆的各位二进制数的和不全为偶数,假定为先手的必胜残局。

 

举个例子:

下面应用此获胜策略来考虑4-堆的Nim取子游戏。其中各堆的大小分别为791215枚硬币。用二进制表示各数分别为:0111100111001111。于是可得到如下一表:

大小为7的堆  0 1 1 1

大小为9的堆  1 0 0 1

大小为12的堆 1 1 0 0

大小为15的堆 1 1 1 1

 

Nim取子游戏的平衡条件可知,此游戏是一个非平衡状态的取子游戏,因此,游戏人I在按获胜策略进行取子游戏下将一定能够取得最终的胜利。具体做法有多种,游戏人I可以从大小为12的堆中取走11枚硬币,使得游戏达到平衡(如下表),

 

大小为7的堆  0 1 1 1

大小为9的堆  1 0 0 1

大小为12的堆 0 0 0 1

大小为15的堆 1 1 1 1

 

之后,无论游戏人II如何取子,游戏人I在取子后仍使得游戏达到平衡。

同样的道理,游戏人I也可以选择大小为9的堆并取走5枚硬币而剩下4枚,或者,游戏人I从大小为15的堆中取走13枚而留下2枚。

归根结底,Nim取子游戏的关键在于游戏开始时游戏处于何种状态(平衡或非平衡)和第一个游戏人是否能够按照取子游戏的获胜策略来进行游戏。

 

原创粉丝点击