HDU1525(Euclid's Game)规律博弈

Problem 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 Input
34 12 15 24 0 0
 
Sample Output
Stan wins Ollie wins
 
题意:a,b两个数字(假设a是大的那个),a可以减去b的若干倍,完成这一步操作后,数字中有零的人获胜。没有则使用新的a和b继续游戏。
 
分析:不管怎么采取决策最终都会有(b,a%b)的局面出现,如果a∈[b, 2*b],那么接下来出现的情况只能是[b,a-b];否则有多种情况。
 

a / b == 1   即a ∈[b , 2b)  , 下一步 只能到(b , a  - b)

a / b >= 2 时, 先手可以选择让谁来面对 (b ,  a%b)这个局势,  显然先手足够聪明, (b , a % b) 这个局势谁胜谁负 已经清楚, 所以先手必胜

假设(a%b)/b>=2,导致后手可以选择对自己有利的局势,那先手就可以把当前局势变为(a%b+b,b),让后手被迫选择对自己有利的局势。同理,如果下面好几个局势都是'a'>2*'b'的,第一个面对a>=2*b的局势的聪明人早就想好了怎么走。。

#include<stdio.h>
#include<algorithm>
using namespace std;
int a,b;
int main()
{
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        if(a==0&&b==0)
            break;
        if(a<b)
        swap(a,b);
        int flag=0;
        while(b)
        {
            if(a%b==0||a/b>=2)
            break;
            a-=b;
            swap(a,b);
            flag=1-flag;
        }
        if(flag)
            printf("Ollie wins\n");
        else
            printf("Stan wins\n");
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/switch-waht/p/11498442.html