John

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

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
 
Recommend
lcy
 

尼姆博奕(Nimm Game):有三堆各若干个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取一个,多者不限,最后取光者得胜。

这种情况最有意思,它与二进制有密切关系,我们用(a,b,c)表示某种局势,首先(0,0,0)显然是奇异局势,无论谁面对奇异局势,都必然失败。第二种奇异局势是
(0,n,n),只要与对手拿走一样多的物品,最后都将导致(0,0,0)。仔细分析一下,(1,2,3)也是奇异局势,无论对手如何拿,接下来都可以变为(0,n,n)的情
形。

计算机算法里面有一种叫做按位模2加,也叫做异或的运算,我们用符号(^)表示这种运算。这种运算和一般加法不同的一点是1^1=0。先看(1,2,3)的按位模2加的结
果:

1 =二进制01
2 =二进制10
3 =二进制11 (^)
———————
0 =二进制00 (注意不进位)

对于奇异局势(0,n,n)也一样,结果也是0。任何奇异局势(a,b,c)都有 a ^ b ^ c =0。如果我们面对的是一个非奇异局势(a,b,c),

要如何变为奇异局势呢?假设 a < b< c,我们只要将 c 变为  a ^ b,即可,因为有如下的运算结果:  a ^ b ^( a ^ b)=(a ^ a) ^ ( b ^ b ) = 0 ^ 0 = 0。

要将c 变为a ^ b,只要从 c中减去 c -(a ^ b)即可。

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int main(){ //freopen("input.txt","r",stdin); int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int ans=,flag=,x;
for(int i=;i<n;i++){
scanf("%d",&x);
ans^=x;
if(x>) //当所有数据都为1时的特判
flag=;
}
if(flag){
if(ans==)
puts("Brother");
else
puts("John");
}else{
if(n&)
puts("Brother");
else
puts("John");
}
}
return ;
}

HDU 1907 John (Nim博弈)的更多相关文章

  1. HDU 1907 John nim博弈变形

    John Problem Description   Little John is playing very funny game with his younger brother. There is ...

  2. HDU 1907 John(博弈)

    题目 参考了博客:http://blog.csdn.net/akof1314/article/details/4447709 //0 1 -2 //1 1 -1 //0 2 -1 //1 2 -1 / ...

  3. hdu 1907 John&& hdu 2509 Be the Winner(基础nim博弈)

    Problem Description Little John is playing very funny game with his younger brother. There is one bi ...

  4. hdu 1907 John (anti—Nim)

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)http://acm.h ...

  5. POJ 3480 &amp; HDU 1907 John(尼姆博弈变形)

    题目链接: PKU:http://poj.org/problem? id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1907 Descri ...

  6. HDU - 1907 John 反Nimm博弈

    思路: 注意与Nimm博弈的区别,谁拿完谁输! 先手必胜的条件: 1.  每一个小游戏都只剩一个石子了,且SG = 0. 2. 至少有一堆石子数大于1,且SG不等于0 证明:1. 你和对手都只有一种选 ...

  7. HDU 1907 John(取火柴博弈2)

    传送门 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int ...

  8. hdu 1907 John(anti nim)

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  9. hdu 1907 John (尼姆博弈)

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

随机推荐

  1. AVR单片机命名规则

    ATmega64 TQFP封装现主要有以下型号:ATmega64L-8AU.ATmega64L-8AI.ATmega64-16AU.ATmega64-16AI. 型号标识说明: (1)带"L ...

  2. webkit-user-select:none 问题

    webkit-user-select:none 问题 学习了:https://bugs.webkit.org/show_bug.cgi?id=82692 最近两天做移动端游戏举报页面.遇到一个问题,移 ...

  3. Eclipse 文件太长,导致着色异常问题

    1. 把C/C++ ->Editor->Scalability, 对应红框中的数字调大.

  4. MFC中的KillTimer

    在Dialog中添加一个Timer. 用如下的代码停用一个Timer总是不好用: void CDialog2::OnTimer (UINT TimerVal) { ////////////////// ...

  5. RefreshListView中onItemClick点击错位

    在使用RefreshListView的时候.发现有使用 /**** * parent.getAdapter().getItem(position)√ * adpter.getItem(id);√ * ...

  6. logback-kafka-appender

    logback 日志写入kafka队列 logback-kafka-appender Logback incompatibility Warning Due to a bug in logback-c ...

  7. ccc如何在一台windows主机上搭建mysql主从复制

    参考:http://www.cnblogs.com/wzjbk/p/6266899.htmlc 进入mysql: 进入到mysql的bin目录下才可以输入 mysql -hlocalhost -uro ...

  8. MySQL 存储过程/游标/事务

    将会用到的几个表 mysql> DESC products; +------------+--------------+------+-----+---------+-------------- ...

  9. 带你走进EJB--将EJB发布为Webservice(2)

    在企业级的应用程序中经常都要把用不同语言写成的.在不同平台上运行的各种程序集成起来,而这种集成将花费很大的开发力量. 简单的一个例子:应用程序经常需要从运行在A主机上的程序中获取数据:或者把数据发送到 ...

  10. 机器学习-分类器-级联分类器训练(Train CascadeClassifier )

    一.简介: adaboost分类器由级联分类器构成,"级联"是指最终的分类器是由几个简单分类器级联组成.在图像检测中,被检窗口依次通过每一级分类器,这样在前面几层的检测中大部分的候 ...