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 ...
随机推荐
- 九度OJ 1034:寻找大富翁 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5925 解决:2375 题目描述: 浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. 输入: 输入包含多组测试用例. ...
- php加速器: xcache
1.准备软件包 xcache-3.2.0.tar.gz 2.安装组件 $ echo 'export LC_ALL=C' >> /etc/profile $ tail -l /etc/pro ...
- TensorFlow运行时出现warning如何设置禁止打印方法
有时候运行TensorFlow时比较烦人,经常报一些warning或者版本更新了之类的一些警告,如下: 那么如何去掉或者说不显示这些东西呢,可以通过简单添加几行代码搞定,来看一下. 这样就不会报警告了 ...
- manacher小结
P3805 [模板]manacher算法 题目大意 n个字符组成的字符串,求最长回文串 $O$$($$n^3$$)$ 枚举两端点,暴力往中间搜 $O$$($$n^2$$)$ 枚举回文串终点,暴力往两边 ...
- Linux Shell Script目录
目录 Linux Shell基础 开始Shell编程 代码 示例代码查看:https://github.com/Furzoom/demo-C/tree/master/src/shell
- linux字符设备学习笔记【原创】
1.申请设备号 int register_chrdev_region(dev_t from, unsigned count, const char *name) 指定从设备号from开始,申请coun ...
- BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戏:部分枚举 位运算
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1647 题意: 在一个n*m(1 <= n,m <= 15)的棋盘上,每一个格子 ...
- JavaScript(3)
var a=90; switch(a){ case "890": window.alert("ok"); break; case 90: window.aler ...
- Smarty模板重点汇总
Smarty模板重点回顾:1.功能:前后端分离:2.实现方法:通过使用Smarty的核心类来实现,利用display方法来读取模板文 件,用正则进行替换,替换完保存到临时文件,再将临时文件加载到当前页 ...
- python学习笔记:第七天(函数)
Python3 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率. 与C一样,Python提供了许多内建函数,比如print().同 ...