UVA1566 LA3830 POJ3480 HDU1907 John【博弈】

John
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3727 Accepted: 1863

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

问题链接UVA1566 LA3830 POJ3480 HDU1907 John
问题简述:n堆石子,两人轮流操作,每次操作只能选定其中一堆,并取走若干个(至少1个),谁取走最后一个谁输。给定一个状态,问先取的赢还是后取的赢?
问题分析:经典的Nim游戏问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA1566 LA3830 POJ3480 HDU1907 John */

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int t, n, a;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);

        int sign = 0, flag = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a);
            if(a != 1) flag = 1;
            sign ^= a;
        }

        if((flag && sign) || (!flag && !sign)) printf("John\n");
        else printf("Brother\n");
    }

    return 0;
}
发布了2289 篇原创文章 · 获赞 2373 · 访问量 265万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105565903
今日推荐