John

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 6017    Accepted Submission(s): 3499

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堆石子,每个人至少去一个,最多把一堆石子取完,取到最后一个石子的人失败

思路

先手胜的情况:

  1. n堆石子全部都只有一个石子,且n堆石子的异或值为0
  2. n堆石子不全是一个石子,且异或值不为0

证明:

  1. 若所有堆石子数都为1且SG值为0,则共有偶数堆石子,故先手胜。
  2. 只有一堆石子数大于1时,我们总可以对该堆石子操作,使操作后石子堆数为奇数且所有堆得石子数均为1
  3. 有超过一堆石子数大于1时,先手将SG值变为0即可,且总还存在某堆石子数大于1

思路来自:http://hzwer.com/1950.html

AC代码

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <limits.h>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <vector>
  11. #include <set>
  12. #include <string>
  13. #define ll long long
  14. #define ull unsigned long long
  15. #define ms(a) memset(a,0,sizeof(a))
  16. #define pi acos(-1.0)
  17. #define INF 0x7f7f7f7f
  18. #define lson o<<1
  19. #define rson o<<1|1
  20. const double E=exp(1);
  21. const int maxn=1e6+10;
  22. const int mod=1e9+7;
  23. using namespace std;
  24. int main(int argc, char const *argv[])
  25. {
  26. ios::sync_with_stdio(false);
  27. int t;
  28. int n;
  29. int x;
  30. cin>>t;
  31. while(t--)
  32. {
  33. cin>>n;
  34. int sum=0;
  35. int res=0;
  36. while(n--)
  37. {
  38. cin>>x;
  39. sum^=x;
  40. if(x>1)
  41. res++;
  42. }
  43. if(!res)
  44. {
  45. if(!sum)
  46. cout<<"John"<<endl;
  47. else
  48. cout<<"Brother"<<endl;
  49. }
  50. else
  51. {
  52. if(!sum)
  53. cout<<"Brother"<<endl;
  54. else
  55. cout<<"John"<<endl;
  56. }
  57. }
  58. return 0;
  59. }

HDU 1907:John(尼姆博弈变形)的更多相关文章

  1. hdu 1907 John (尼姆博弈)

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

  2. hdu 1907 (尼姆博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1907 Problem Description Little John is playing very ...

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

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

  4. hdu 1849 (尼姆博弈)

    http://acm.hdu.edu.cn/showproblem.php? pid=1849 简单的尼姆博弈: 代码例如以下: #include <iostream> #include ...

  5. HDU 1907 John (Nim博弈)

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

  6. John 尼姆博弈

    John Little John is playing very funny game with his younger brother. There is one big box filled wi ...

  7. HDU - 1907 John 反Nimm博弈

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

  8. hdu 1907 尼姆博弈

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

  9. HDU.1850 being a good boy in spring festival (博弈论 尼姆博弈)

    HDU.1850 Being a Good Boy in Spring Festival (博弈论 尼姆博弈) 题意分析 简单的nim 博弈 博弈论快速入门 代码总览 #include <bit ...

随机推荐

  1. vs 编译库文件 Qt编译库文件

    QT 库能不能用 需要关注是minGW 还是MSVC编译的 Qt MinGW与MSVC对比  转:https://blog.csdn.net/u013185164/article/details/48 ...

  2. Ubuntu中的在文件中查找和替换命令

    分类: 9.Linux技巧2009-09-29 13:40 1429人阅读 评论(0) 收藏 举报 ubuntujdbc 1.查找 find /home/guo/bin -name /*.txt | ...

  3. Fiddler系列教程1:初识Http协议抓包工具

    1. Fiddler简介 Fiddler是用一款使用C#编写的http协议调试代理工具.它支持众多的http调试任务,能够记录并检查所有你的电脑和互联网之间的http通讯,可以设置断点,查看所有的“进 ...

  4. mybatis if标签判断字符串相等

    mybatis 映射文件中,if标签判断字符串相等,两种方式: 因为mybatis映射文件,是使用的ognl表达式,所以在判断字符串sex变量是否是字符串Y的时候, <if test=" ...

  5. nginx+tomcat集群

    参考: 简单:http://blog.csdn.net/wang379275614/article/details/47778201 详细:http://www.jb51.net/article/77 ...

  6. sqlserver指定排序字段

    在sqlserver中可以指定排序的字段,需要将哪个字段值排在最前面或最后面,都是可以的.见如下代码: SELECT * FROM public_comment order by case [User ...

  7. (C/C++学习笔记) 一. 基础知识

    一. 基础知识 ● 程序和C/C++ 程序: 根据Wirth (1976), Algorithms + Data Structures = Programs. Whence C: 1972, Denn ...

  8. 在ros功能包CMakeLists.txt中获取所在功能包的路径 便于添加第三方库的相对路径

    在 ros 功能包中要使用第三方的动态库,将其放在系统默认库路径和使用绝对路径均不可取,这样的话可移植性较差,将该功能包移到其它电脑时要重新配置依赖库的路径,太麻烦了. 于是找到下面这个方法,解决了R ...

  9. python笔记3-输出输入、字符串格式化

    输入.输出 python怎么来接收用户输入呢,使用input函数,python2中使用raw_input,接收的是一个字符串,输出呢,第一个程序已经写的使用print,代码入下: 1 2 name=i ...

  10. python学习二三事儿(转,整)

    Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...