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;
    }
  • 相关阅读:
    Linux 文件误删 恢复
    RSS源 集合
    查询android系统数据库 自定义sql语句 办法
    Android通讯录数据库介绍与基本操作(增删改查)
    Android 监听短信 两种方式
    获取系统设置好的闹钟时间
    颜色 16进制对照表
    仿IPhone 长按图标删除应用,图标抖动效果
    配置文件的加载顺序和位置
    php7 使用dom动态生成xml文档
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8404655.html
Copyright © 2011-2022 走看看