POJ 3480 & HDU 1907 John(尼姆博弈变形)

题目链接:

PKU:http://poj.org/problem?id=3480

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1907

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

PS:

尼姆博弈变形!

代码如下:

#include <cstdio>
#define maxn 5017
int main()
{
    int t;
    int a[maxn];
    int n, s, k;
    scanf("%d",&t);
    while(t--)
    {
        s = 0, k = 0;
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i] > 1)
                k++;
            s = s^a[i];
        }
        if(k == 0)//全为1
        {
            if(n%2 == 0)//偶数堆
                printf("John\n");
            else
                printf("Brother\n");
        }
        else
        {
            if(s)
                printf("John\n");
            else
                printf("Brother\n");
        }
    }
    return 0;
}
时间: 2024-08-10 16:57:05

POJ 3480 & HDU 1907 John(尼姆博弈变形)的相关文章

hdu 1849 (尼姆博弈)

http://acm.hdu.edu.cn/showproblem.php?pid=1849 简单的尼姆博弈: 代码如下: #include <iostream> #include <cstdio> using namespace std; int main() { int m,n,t; while(cin>>m,m) { int ans=0; for(int i=0; i<m; i++) { cin>>n; ans^=n; } if(ans==0)

John 尼姆博弈

John 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

POJ2234 Matches Game 尼姆博弈 博弈论

http://poj.org/problem?id=2234 尼姆博弈(Nimm's Game) 指的是这样一个博弈游戏:有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品中取物品,最少取一件,取到最后一件物品的人获胜. 结论:把每堆物品数全部异或起来,如果得到的值为0,那么先手必败,否则先手必胜. 直接判定即可. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #

hdu 1907 尼姆博弈

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3745    Accepted Submission(s): 2116 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

HDU 1907 John(博弈)

题目 参考了博客:http://blog.csdn.net/akof1314/article/details/4447709 //0 1 -2 //1 1 -1 //0 2 -1 //1 2 -1 //2 2 -2 //0 3 -1 //1 3 -1 //2 3 -1 //3 3 -2 //0 4 -1 //1 4 -1 //2 4 -1 //3 4 -1 //4 4 -2 //0 5 -1 //1 5 -1 //2 5 -1 //3 5 -1 /* 尼姆博弈.对于N堆的糖,一种情况下是每堆都是

hdu 1850 Being a Good Boy in Spring Festival (尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4658    Accepted Submission(s): 2781 Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场悄悄给爸爸买

HDU 4315 Climbing the Hill (尼姆博弈)

Climbing the Hill Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Alice and Bob are playing a game called "Climbing the Hill". The game board consists of cells arranged vertically, as the

题解报告:hdu 1850 Being a Good Boy in Spring Festival(尼姆博弈)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1850 Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧陪妈妈逛一次菜场悄悄给爸爸买个小礼物主动地 强烈地 要求洗一次碗某一天早起 给爸妈用心地做回早餐如果愿意 你还可以和爸妈说咱们玩个小游戏吧 ACM课上学的呢-下面是一个二人小游戏:桌子上有M堆扑克牌:每堆牌的数量分别为Ni(i=1-M):两人轮流进行:每走一步可以任意选择一堆并取

hdu 1849(Rabbit and Grass) 尼姆博弈

Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3864    Accepted Submission(s): 2919 Problem Description 大学时光是浪漫的,女生是浪漫的,圣诞更是浪漫的,但是Rabbit和Grass这两个大学女生在今年的圣诞节却表现得一点都不浪漫:不去逛商场,不去逛