• hdu 1907 John / 2509 Be the Winner 博弈 最后取完者为输


    原理引入:

    取火柴的游戏
    题目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<iostream>
    #include<stdlib.h>
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<set>
    #define Inf 0x7fffffff // 0x 是数字0,而不是字母o
    #define N 15
    using 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<n;i++)
           {
               cin>>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<iostream>
    #include<stdlib.h>
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<set>
    #define Inf 0x7fffffff // 0x 是数字0,而不是字母o
    #define N 15
    using namespace std;
    int sg[N];
    int main(){
       int t,n,m;
    
       while(cin>>n)
       {
           int x=0,unalone=0;
           for(int i=0;i<n;i++)
           {
               cin>>m;
               x^=m;
               if(m!=1) unalone++;
    
           }
           if((x==0 && unalone>=2) || (x&&unalone==0))  // 必败态 T2,S0
                puts("No");
            else puts("Yes");
       }
        return 0;
    }
  • 相关阅读:
    VC开发,引用win8.1配置
    RabbitMQ——常见的函数介绍
    OpenLayer4——面(多边形)
    OpenLayer4——图形组合
    OpenLayer4——GeoJSON简介
    RabbitMQ——交换机
    RabbitMQ——helloworld
    OpenLayer4——添加Geojson
    RabbitMQ——消息头
    OpenLayer4——圆形
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3612040.html
Copyright © 2020-2023  润新知