zoukankan      html  css  js  c++  java
  • HDU 1525 Euclid's Game (博弈)

    HDU 1525 Euclid's Game (博弈)

    Description

            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.

    Input

            The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.

    Output

            For 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

    Sample Input
    34 12 15 24 0 0 Sample Output
    Stan wins Ollie wins

    题意:

            题目给出了两个正数a.b

            每次操作,大的数减掉小的数的整数倍。一个数变为0 的时候结束。

            谁先先把其中一个数减为0的获胜。问谁可以赢。Stan是先手。

    思路:

            假设一个这样的状态T1=(x,y),并且x>y,
            那么状态T2=(x+y,y)可以到达T1,并且两者之间一个为必胜状态,那么另外一个为必败状态。

            但是,T3=(x+2y,y)既可以到达T1状态,又可以到达T2状态,那么我们可以说T3状态是一个必胜状态,
    同理T4=(x+3y,y),...(x+4y,y)等几个状态,也同样可以到达T1与T2状态,那么这些状态也为必胜状态
            所以我们说如果满足x>=2y,则为必胜状态

            如果是y<x<2y,这种情况,下个状态只能为(x-y,y),为了保证前面一个数大于后面一个数,我们会将两者交换
    x不可能小于y,因为前提假设了x>y

            如果x==y,或者x=0或者y=0,那么当前取石子的人胜利

    代码:

    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    int main()
    {
        int a,b;
        while(1)
        {
            int i;
            scanf("%d%d",&a,&b);
            if(a==0||b==0) break;
            for(i=0;;i++)
            {
                if(a<=b)
                {
                    if(b%a==0||(b-a>=a))break;//这里最好不要用b>=2*a,因为2*a可能会超过int,也可以改为long long用b>=2*a;
                    else b-=a;
                }
                else
                {
                    if(a%b==0||(a-b>=b)) break;
                    else a-=b;
                }
            }
            if(i%2==0)
                printf("Stan wins
    ");
            else
                printf("Ollie wins
    ");
        }
    }
  • 相关阅读:
    燃尽的一个不便之处修改
    jquery给元素添加样式表的方法
    http协议
    春节读书
    file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known
    jQuery on(),live(),trigger()
    jquery ui中的dialog,官网上经典的例子
    问题:循环元素,被选中元素个数,全选
    3.5星|《小学问》:年轻人思维与婚恋常见误区解析
    正念疗法入门介绍,作者是日籍美国正念疗法医生:3星|《高效休息法》
  • 原文地址:https://www.cnblogs.com/aiguona/p/7183310.html
Copyright © 2011-2022 走看看