John

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
 
题意:
  今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根, 可将一堆全取走,但不可不取,最后取完者为负。
题解:
  T态:异或和为0+
  S态:异或和不为0+
  定义:若一堆中仅有1根火柴,则被称为孤单堆。若大于1根,则称为充裕堆。
  定义:T态中,若充裕堆的堆数大于等于2,则称为完全利他态,用T2表示;若充裕堆的堆数等于0,则称为部分利他态,用T0表示。
 
孤单堆的根数异或只会影响二进制的最后一位,但充裕堆会影响高位(非最后一位)。一个充裕堆,高位必有一位不为0,则所有根数异或不为0。故不会是T态。
[定理5]:S0态,即仅有奇数个孤单堆,必败。T0态必胜。 
证明:
S0态,其实就是每次只能取一根。每次第奇数根都由己取,第偶数根都由对 
方取,所以最后一根必己取。败。同理,  T0态必胜#
[定理6]:S1态,只要方法正确,必胜。 
证明:
若此时孤单堆堆数为奇数,把充裕堆取完;否则,取成一根。这样,就变成奇数个孤单堆,由对方取。由定理5,对方必输。己必胜。  # 
[定理7]:S2态不可转一次变为T0态。 
证明:
充裕堆数不可能一次由2变为0。得证。  #

[定理8]:S2态可一次转变为T2态。 
证明:
由定理1,S态可转变为T态,态可一次转变为T态,又由定理6,S2态不可转一次变为T0态,所以转变的T态为T2态。  # 
[定理9]:T2态,只能转变为S2态或S1态。 
证明:
由定理2,T态必然变为S态。由于充裕堆数不可能一次由2变为0,所以此时的S态不可能为S0态。命题得证。 
[定理10]:S2态,只要方法正确,必胜. 
证明:
方法如下: 
      1)  S2态,就把它变为T2态。(由定理8) 
      2)  对方只能T2转变成S2态或S1态(定理9)
    若转变为S2,  转向1) 
    若转变为S1,  这己必胜。(定理5) 
[定理11]:T2态必输。 
证明:同10。 
综上所述,必输态有:  T2,S0 
          必胜态:    S2,S1,T0.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+, M = 2e5+, mod = 1e9+, inf = 2e9; int sg[N],n,x,ans,vis[N];
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
int ans = , cnt = ;
for(int i = ; i <= n; ++i) {
scanf("%d",&x);
ans = ans ^ x;
if(x > ) cnt++;
}
if((ans && cnt >= ) || (cnt == ) || (cnt == && !ans)) puts("John");
else puts("Brother");
}
return ;
}

HDU 1907 John nim博弈变形的更多相关文章

  1. HDU 1907 John(博弈)

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

  2. HDU 1907 Nim博弈变形

    1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形. ...

  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 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 ...

  5. HDU 2509 Nim博弈变形

    1.HDU 2509  2.题意:n堆苹果,两个人轮流,每次从一堆中取连续的多个,至少取一个,最后取光者败. 3.总结:Nim博弈的变形,还是不知道怎么分析,,,,看了大牛的博客. 传送门 首先给出结 ...

  6. POJ1704 Georgia and Bob(Nim博弈变形)

    Georgia and Bob Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14312   Accepted: 4840 ...

  7. HDU 1907 John (Nim博弈)

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

  8. hdu 1907 John (anti—Nim)

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

  9. HDU 3032 (Nim博弈变形) Nim or not Nim?

    博弈的题目,打表找规律还是相当有用的一个技巧. 这个游戏在原始的Nim游戏基础上又新加了一个操作,就是游戏者可以将一堆分成两堆. 这个SG函数值是多少并不明显,还是用记忆化搜索的方式打个表,规律就相当 ...

随机推荐

  1. [bigdata] 使用Flume hdfs sink, hdfs文件未关闭的问题

    现象: 执行mapreduce任务时失败 通过hadoop fsck -openforwrite命令查看发现有文件没有关闭. [root@com ~]# hadoop fsck -openforwri ...

  2. linux下centos的网络连接

  3. web前端基础知识-(三)JavaScript基本操作

    JavaScript 是一种轻量级的编程语言. JavaScript 是可插入 HTML 页面的编程代码. JavaScript 插入 HTML 页面后,可由所有的现代浏览器执行. JavaScrip ...

  4. Git------Win7系统使用TortoiseGit

    转载: https://my.oschina.net/longxuu/blog/141699?p=1 此步可以省略 1.点击TortoiseGit->PuTTygen 2.点击"Gen ...

  5. ASP.NET中使用JqGrid完整实现

    文章提纲 介绍 & 使用场景 JqGrid的一些说明 JqGrid和ASP.NET整合详细步骤 前置准备 框架搭建 数据填充 数据增/删/改 其他 介绍&使用场景 JqGrid不是一个 ...

  6. unity知识点思维导图

    写了个思维导图,总结了下学习unity的知识点感觉还有其他很多的没写到,等我慢慢在工作中完善它,这是下面的链接,后续会根据他的每一个细节来丰富我的博客. 详细地址: http://naotu.baid ...

  7. jQuery知识点一 each()和toggleClass()

    jQuery的一些东东比较容易忘,所以在这里整理一下... ... 1. each (1) $(selector).each(function(index,element))         inde ...

  8. 公众平台关注用户达到5万即可开通流量主功能 可以推广APP应用

    今天微信公众平台发布发布了一些更新,公众帐号的关注用户达到5万,即可开通流量主功能,之前的是要求10万粉丝,这是一个微信开放的信号.广告主可推广苹果商店应用或腾讯开放平台应用.新增卡片和图文广告规格. ...

  9. TCP/IP 协议

    网站: http://blog.csdn.net/goodboy1881/article/category/204448

  10. 巧用dimens适配多个分辨率

      让应用自动适配多个分辨率的屏幕,是每个android程序员的基本功,就好像前端工程师熟练编写CSS Hack一样.适配工作中一个重要的工作就是对页面的调整. 对于页面的适配,有很多的方法和技巧.比 ...