John

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

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

题意:有n给糖果,每种有ai颗,两个人每次都从一堆中吃几颗,不能不吃。吃掉最后一颗的人算输。John先吃,问最后谁会赢。

题解:nim博弈。先手必胜的结论有两个:(1)当所有种类糖果数量都是1的时候,就先手必胜,因为你拿一个我拿一个,最后一个肯定是另一个人拿的。(2)有充裕堆(存在一堆中的糖果数大于1的情况)的时候,异或和为0,先手必败,不为0,先手必胜。

反nim博弈的结论

 #include<bits/stdc++.h>
using namespace std;
int main() {
int t;
while(~scanf("%d",&t))
{
while(t--)
{
int n;
scanf("%d",&n);
int ai;
int ans=;int num=;
for(int i=;i<n;i++)
{
scanf("%d",&ai);
ans=ans^ai;
if(ai>)num++;
}
if(num)//有充裕堆,异或和不为0胜
{
if(ans==)printf("Brother\n") ;
else printf("John\n");
}
else
{
if(ans==)//有偶数个,且每个都为1
{
printf("John\n");
}else
{
printf("Brother\n") ;
}
}
}
}
return ;
}

hdu1907John(反nim博弈)的更多相关文章

  1. hdu2509Be the Winner(反nim博弈)

    Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. LightOJ 1253 Misere NIM(反NIM博弈)

    Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, eac ...

  3. 反Nim博弈

    原文地址:https://blog.csdn.net/xuejye/article/details/78975900 在尼姆博奕中取完最后一颗糖的人为赢家,而取到最后一颗糖为输家的就是反尼姆博奕.这道 ...

  4. 博弈论中的Nim博弈

    瞎扯 \(orzorz\) \(cdx\) 聚聚给我们讲了博弈论.我要没学上了,祝各位新年快乐.现在让我讲课我都不知道讲什么,我会的东西大家都会,太菜了太菜了. 马上就要回去上文化课了,今明还是收下尾 ...

  5. Nim博弈&&POJ1704

    Nim博弈 题目 有n堆物品,两人轮流取,每次取某堆中不少于1个,先取完者胜. 分析 经典问题,该问题的策略也成为了许多问题的基础. 要判断游戏的胜负只需要异或运算就可以了,有以下结论: $a_1 \ ...

  6. HDU 2509 Nim博弈变形

    1.HDU 2509  2.题意:n堆苹果,两个人轮流,每次从一堆中取连续的多个,至少取一个,最后取光者败. 3.总结:Nim博弈的变形,还是不知道怎么分析,,,,看了大牛的博客. 传送门 首先给出结 ...

  7. HDU 1907 Nim博弈变形

    1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形. ...

  8. zoj3591 Nim(Nim博弈)

    ZOJ 3591 Nim(Nim博弈) 题目意思是说有n堆石子,Alice只能从中选出连续的几堆来玩Nim博弈,现在问Alice想要获胜有多少种方法(即有多少种选择方式). 方法是这样的,由于Nim博 ...

  9. BZOJ_1022_[SHOI2008]_小约翰的游戏John_(博弈论_反Nim游戏)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1022 反Nim游戏裸题.详见论文<组合游戏略述——浅谈SG游戏的若干拓展及变形>. ...

随机推荐

  1. Junit单元测试多线程的问题

    今天下午很快完成了一个接口的监控功能,然后屁颠屁颠地用Junit开始单元测试.然后我就开始陷入崩溃的边缘... 监控结束后需要将监控结果以邮件的形式发送给运营的小伙伴维护,前面测试还是很顺利,到了开多 ...

  2. zabbix-agent安装

    1.下载yum源库 rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm ...

  3. 【luogu P2279 [HNOI2003]消防局的设立】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2279 想怎么贪怎么贪 #include <queue> #include <cstdio& ...

  4. 图片左右滚动控件(带倒影)——重写Gallery

    转http://blog.csdn.net/ryantang03/article/details/8053643 今天在网上找了些资料,做了一个图片左右滚动的Demo,类似幻灯片播放,同时,图片带倒影 ...

  5. 【题解】洛谷P1074 [NOIP2009TG] 靶形数独(DFS+剪枝)

    洛谷P1074:https://www.luogu.org/problemnew/show/P1074 思路 这道题一看就是DFS 打一个分数表方便后面算分 我用x y z数组分别表示行 列 宫 是否 ...

  6. ARM 内核 汇编指令 的 8种 寻址方式

    str: store register ->指令将寄存器内容存到内存空间中, ldr:  load register 将内存内容加载到通用寄存器, ldr/str 组合来实现ARM CPU 和内 ...

  7. 在js中获取request域中的内容

    1.可以使用小脚本<%%>实现: var pro_id=<%request.getPro_id()%>; 2.使用隐藏域实现: <input type="hid ...

  8. 架构风格:你真的懂REST吗?

    本文探讨如下几个问题: 什么是REST REST包含哪些约束 什么是RESTful 纯RESTful API的难点在哪里 如果你去搜索「什么是REST」的话,大部分情况下,你看到的基本都是RESTfu ...

  9. ObjC之RunTime(上)

    转载自这里. 最近看了一本书——iOS6 programming Pushing the Limits(亚马逊有中文版),最后一章是关于Deep ObjC的,主要内容是ObjC的runtime.虽然之 ...

  10. 技巧:Vimdiff 使用(改)

    技巧:Vimdiff 使用(改) 各种 IDE 大行其道的同时,传统的命令行工具以其短小精悍,随手可得的特点仍有很大的生存空间,这篇短文介绍了一个文本比较和合并的小工具:vimdiff.希望能对在 U ...