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. 条件随机场(CRF)原理和实现

    版权声明:作者:金良山庄,欲联系请评论博客或私信,个人主页:http://www.jinliangxu.com/,CSDN博客: http://blog.csdn.net/u012176591   目 ...

  2. IIS配置中出现HRESULT:0X80070020错误

    Win7 IIS启动失败.手工启动它,提示:“另一个程序正在使用此文件,进程无法访问!” 此时是因为另一个程序占用了IIS的端口号,IIS一般用的是80端口,是谁占用了这个端口呢? 方法如下:开始菜单 ...

  3. Spring(十七):Spring AOP(一):简介

    背景: 需求: 给一个计算器计算函数执行前后添加日志. 实现: 1)直接在函数中修改代码: IArithmeticCalculator.java接口类 package com.dx.spring.be ...

  4. GOOD BLOG URL

    1TEST http://www.cnblogs.com/Javame/p/3653509.html 综合 http://shiyanjun.cn/

  5. 【转】Java抽象类与接口的区别

    很多常见的面试题都会出诸如抽象类和接口有什么区别,什么情况下会使用抽象类和什么情况你会使用接口这样的问题.本文我们将仔细讨论这些话题. 在讨论它们之间的不同点之前,我们先看看抽象类.接口各自的特性. ...

  6. 解决IE6兼容性问题的十一大技巧

    10要点解决IE6兼容性问题 1.使用声明 你必须经常在html网页头部放置一个声明,推荐使用严格的标准.例如 <!DOCTYPEHTMLPUBLIC“-//W3C//DTDHTML4.01// ...

  7. tar 使用总结

    今天需要备份数据,使用tar命令,总结一下. 压缩命令: tar -zvvf ticket-data-intgration.tar.gz ticket-data-intgration 压缩但是不包含某 ...

  8. spring + mybatis合集

    一.Spring 1. IoC 什么是IoC: 跟我一起学Spring 3(4)–深入理解IoC(控制反转)和DI(依赖注入) Spring中IoC的优点与缺点 spring Ioc 实践 IoC如何 ...

  9. 手把手教你从零实现Linux misc设备驱动一(基于友善之臂4412开发板)

    关于怎样来写一个misc设备,在前面有篇文章已经介绍了大致的流程,如今就让我们来实现一个最简单的misc设备驱动. http://blog.csdn.net/morixinguan/article/d ...

  10. 好久不git这么多问题

    本来想把本地项目上传GitHub一下,打开gitbash, git init 之前配置过了 用户名和邮箱以及ssh等 $ git remote add origin https://github.co ...