zoukankan      html  css  js  c++  java
  • HDU1525 Euclid's Game

    Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Ollie, the second player, does the same with the two resulting numbers, then Stan, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7): 


    25 7 
    11 7 
    4 7 
    4 3 
    1 3 
    1 0 

    an Stan wins. 

    InputThe input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.OutputFor each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed. 

    Sample Input

    34 12
    15 24
    0 0

    Sample Output

    Stan wins
    Ollie wins

    题意:
    两人博弈,给出两个数a和b,较大数减去较小数的任意倍数,结果不能小于0,将两个数任意一个数减到0的为胜者。

    错误思路:

    最开始我是这样想的:(a,b)->(b,a%b),算一个转移,中间可以减a/b次;(b,a%b)->(a%b,b%(a%b))需要..次。把每一个转移看成一个堆,那么每堆可以任选,就成了Nim博弈了。代码如下:

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    int main()
    {
        int Xor,a,b;
        while(~scanf("%d%d",&a,&b)){
            if(a==0&&b==0) return 0;
            Xor=0;
            while(true){
                if(a==0||b==0) break;
                if(a<b) swap(a,b);
                Xor=Xor^(a/b);
                a=a%b;
            }
            if(Xor) printf("Stan wins
    ");
            else printf("Ollie wins
    ");
        } return 0;
    }
    View Code

    但是这样做是有问题的,因为Nim博弈是任选一堆的任意个,而这样转化的从前往后的堆里选任意个。所以不一样。

    正确打开方式:   假设a大于b a == b.  N态 a%b == 0. N态 

                                 a >= 2*b,先手能决定谁取(b,a%b),并且知道(b,a%b)是P态还是N态.    N态

                                 b<a<2*b, 只能 -->(b,a-b) , 然后再进行前面的判断.

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    int main()
    {
        int num,a,b;
        while(~scanf("%d%d",&a,&b)){
            if(a==0&&b==0) return 0;
            num=1;
            while(true){
                if(a<b) swap(a,b);
                if(a%b==0||b==0) break;
                if(a>=2*b) break;
                a=a%b;
                num++;
            }
            if(num&1) printf("Stan wins
    ");
            else printf("Ollie wins
    ");
        } return 0;
    }
  • 相关阅读:
    ASP 调用dll(VB)及封装dll实例
    C#中的Dictionary简介
    C#中Dictionary、ArrayList、Hashtable和Array的区别
    类 Array Arraylist List Hashtable Dictionary
    利用ASP.NET AJAX的Timer讓GridView每隔一段時間做到自動換頁的功能
    nvmw install 失败. 需修改"Msxml2.XMLHTTP"为"Msxml2.ServerXMLHTTP"
    cyark
    [001] winnie the pooh
    把你的英语用起来-七天行动-置之死的而后生
    安卓模拟器还是"genymotion"最靠谱.
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8404655.html
Copyright © 2011-2022 走看看