博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1907 John / 2509 Be the Winner 博弈 最后取完者为输
阅读量:6907 次
发布时间:2019-06-27

本文共 4376 字,大约阅读时间需要 14 分钟。

原理引入:

取火柴的游戏

题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根, 
可将一堆全取走,但不可不取,最后取完者为胜,求必胜的方法。 
题目2:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若干根, 
可将一堆全取走,但不可不取,最后取完者为负,求必胜的方法。
嘿嘿,这个游戏我早就见识过了。小时候用珠算玩这个游戏:第一档拨一个,第二档拨两个,依次直到第五档拨五个。然后两个人就轮流再把棋子拨下来,谁要是最后一个拨谁就赢。有一次暑假看见两个小孩子在玩这个游戏,我就在想有没有一个定论呢。下面就来试着证明一下吧
先解决第一个问题吧。
定义:若所有火柴数异或为0,则该状态被称为利他态,用字母T表示;否则, 
为利己态,用S表示。
[定理1]:对于任何一个S态,总能从一堆火柴中取出若干个使之成为T态。
[定理2]:T态,取任何一堆的若干根,都将成为S态。
[定理 3]:S态,只要方法正确,必赢。 
  
[定理4]:T态,只要对方法正确,必败。 
接着来解决第二个问题。

定义:若一堆中仅有1根火柴,则被称为孤单堆。若大于1根,则称为充裕堆。
定义:T态中,若充裕堆的堆数大于等于2,则称为完全利他态,用T2表示;若充裕堆的堆数等于0,则称为部分利他态,用T0表示。

由此, 我们知道  

S0 :  a1^a2^...^an !=0  && 充裕堆为0(即全是孤单堆,而且显然是 奇数个) 必败。

 T0:    a1^a2^...^an =0  && 充裕堆为0(即偶数个 孤单堆), 必胜。

S2:  a1^a2^...^an !=0  && 充裕堆>=2 ,  必胜。

T2:a1^a2^...^an =0  && 充裕堆>=2 ,  必败。

S1: a1^a2^...^an !=0  && 充裕堆=1 , 必胜。

因此:必败态有, T2, S0, 必胜态有: S2,S1,T0.

孤单堆的根数异或只会影响二进制的最后一位,但充裕堆会影响高位(非最后一位)。一个充裕堆,高位必有一位不为0,则所有根数异或不为0。故不会是T态。

[定理5]: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. 
两题比较: 
第一题的全过程其实如下: 
S2->T2->S2->T2->  ……  ->T2->S1->T0->S0->T0->……->S0->T0(全0) 
第二题的全过程其实如下: 
S2->T2->S2->T2->  ……  ->T2->S1->S0->T0->S0->……->S0->T0(全0) 
下划线表示胜利一方的取法。  是否发现了他们的惊人相似之处。 
我们不难发现(见加黑部分),S1态可以转变为S0态(第二题做法),也可以转变为 
T0(第一题做法)。哪一方控制了S1态,他即可以有办法使自己得到最后一根(转变为 
T0),也可以使对方得到最后一根(转变为S0)。 
  所以,抢夺S1是制胜的关键! 
  为此,始终把T2态让给对方,将使对方处于被动状态,他早晚将把状态变为S1.

hdu 1907

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1907

John

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

Total Submission(s): 2490    Accepted Submission(s): 1354

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
 
 
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Inf 0x7fffffff // 0x 是数字0,而不是字母o#define N 15using namespace std;int sg[N];int main(){ int t,n,m; cin>>t; while(t--) { cin>>n; int x=0,unalone=0; for(int i=0;i
>m; x^=m; if(m!=1) unalone++; } if((x==0 && unalone>=2) || (x&&unalone==0)) // 必败态 T2,S0 puts("Brother"); else puts("John"); } return 0;}

 hdu  2509 http://acm.hdu.edu.cn/showproblem.php?pid=2509

分析:   最后取玩苹果的为输  ,  我们知道 先手必输态 P_position    为 T2,S0

代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Inf 0x7fffffff // 0x 是数字0,而不是字母o#define N 15using namespace std;int sg[N];int main(){ int t,n,m; while(cin>>n) { int x=0,unalone=0; for(int i=0;i
>m; x^=m; if(m!=1) unalone++; } if((x==0 && unalone>=2) || (x&&unalone==0)) // 必败态 T2,S0 puts("No"); else puts("Yes"); } return 0;}

 

 

转载于:https://www.cnblogs.com/zn505119020/p/3612040.html

你可能感兴趣的文章
JS实现密码加密
查看>>
HTML+CSS-如何定义让两个div横向排列
查看>>
Matlab画柱状和折线对照图
查看>>
javascript时间戳和日期字符串相互转换
查看>>
链接详解--静态库
查看>>
从0开始学java——JUnit4 复习,其实基本思想还是那些,不过采用了新的注释格式的语法...
查看>>
GNU M4 - GNU Project - 免费软件基金会(FSF)
查看>>
jsp中将后台传递过来的json格式的list数据绑定到下拉菜单select
查看>>
Project Euler 85 :Counting rectangles 数长方形
查看>>
MYSQL查询某字段中以逗号分隔的字符串的方法
查看>>
Excel设置下拉菜单并隐藏下拉菜单来源单元格内容
查看>>
Java8初体验(二)Stream语法详解
查看>>
微服务架构——不是免费的午餐
查看>>
基于HTML5的Web SCADA工控移动应用
查看>>
VS 2015相当不错的功能:C#交互窗口
查看>>
hive复杂类型与java类型的对应
查看>>
[Ubuntu] ubuntu10.04系统维护之Wine的安装
查看>>
iOS获取UIView上某点的颜色值
查看>>
cocos2d-x 3.0 android mk文件 之 自己主动遍历*.cpp文件
查看>>
python数字图像处理(7):图像的形变与缩放
查看>>