John 尼姆博弈
John
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.
InputThe 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
OutputOutput 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堆的糖,一种情况下是每堆都是1,那么谁输谁赢看堆数就知道;
对于不都是1的话,若这些堆是奇异局势,或说他们是非奇异局势,但非奇异局势皆可以转换到奇异局势。 经典的尼姆问题是谁哪拿到最后一个则谁赢,本题是拿最后一个的输。
下面分析第二种情况:
1.初始给的是奇异局势的话,则先取者拿到最后一个为输。
2.初始给的是非奇异局势的话,则先取者为赢。
对于任何奇异局势(a,b,c),都有a^b^c=0(^是代表异或).
非奇异局势(a,b,c)(a<b<c)转换为奇异局势,只需将c变成a^b,即从c中减去c-(a^b)即可
import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args){ int t, n, x, f, ans, i;
t = sc.nextInt();
while((t--) != 0){
n = sc.nextInt();
ans = 0; f = 0;
for(i = 1; i <= n; i++){
x = sc.nextInt();
ans ^= x;
if(x > 1) f = 1;
}
if(f == 0){
if((n & 1) != 0) System.out.println("Brother");
else System.out.println("John");
}
else{
if(ans == 0) System.out.println("Brother");
else System.out.println("John");
}
}
}
}
John 尼姆博弈的更多相关文章
- hdu 1907 John (尼姆博弈)
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- POJ 3480 & HDU 1907 John(尼姆博弈变形)
题目链接: PKU:http://poj.org/problem? id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1907 Descri ...
- hdu 1907 (尼姆博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1907 Problem Description Little John is playing very ...
- HDU1907(尼姆博弈)
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- hdu 1907 尼姆博弈
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- hdu1907 尼姆博弈
尼姆博弈的性质. 最后一个取输.若a1^a2^a3...^a4=0表示利他态T,不然为利己态S.充裕堆:1个堆里的个数大于2.T2表示充裕堆大于等于2,T1表示充裕堆大于等于1,T0表示无充裕堆.S2 ...
- hdu----(1849)Rabbit and Grass(简单的尼姆博弈)
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1849(Rabbit and Grass) 尼姆博弈
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Being a Good Boy in Spring Festival 尼姆博弈
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
随机推荐
- Chrome 的滚动条修改.
该方法针对于win下Chrome任何版本(未测试基于Chrome内核的其他浏览器),Lunix就是目录换了一下 目录是:**\Google\Chrome\User Data\Profile 2\Use ...
- N皇后问题算法
N皇后问题的两种主要算法是试探回溯法和位运算法.前一种是经典算法,后一种是目前公认的最高效算法,后者比前者效率提高了至少一个数量级.很多问题可以借鉴位运算的思想. 以下是转载的我认为写的比较好的一篇N ...
- 【CISCO强烈推荐】生成树 《路由协议》 卷一二 拥塞:网络延迟 阻塞:进程中 MTU QS:服务质量 OSPF RIP ISIS BGP 生成树 《路由协议》 卷一二
协议 CP/IP路由技术第一卷 作 者 (美)多伊尔,(美)卡罗尔
- Redis之java增删改查
jedis是java的redis客户端实现,要使用jedis须要加入jedis的maven依赖: <dependency> <groupId>redis.clients< ...
- flex做页面。用来做视频的后台服务器是fms
作为新一代的富客户端互联网技术的佼佼者,Flex这种技术已经被越来越多的公司所采用,被越来越多的用户和程序员所接受.以下列出Flex十大优势: 1.Flex与Flash:可以让普通程序员开发制作Fla ...
- JAVA线程sleep和wait方法区别 代码
package test; import java.util.Date; import java.util.Random; public class test { public static void ...
- 20145239 《Java程序设计》实验三 实验报告
详见我的parter20145224的博客:http://www.cnblogs.com/20145224kevs/p/5428892.html 感想:这次的实验看似容易,但很多点都需要注意,比如开源 ...
- C语言“快速排序”函数写法
代码是:C语言中快速排的写法,要加入头文件 <stdlib.h> qsort(数组名, 长度, 数据类型大小,比较算子 ): #include <stdio.h> #inc ...
- Appium——元素定位
首先介绍两种定位元素的工具,appium自带的 Inspector 和 android SDK自带的 uiautomatorviewer 1.UIAutomator Viewer比较简单,在模拟器打开 ...
- html5手机网站需要加的那些meta标签,手机网站自适应
的html5相关meta和标签 a.<!-- 强制让文档与设备的宽度保持1:1 --> <meta name="viewport" content=& ...