Euclid's Game (博弈论)

Euclid's Game

 HDU - 1525 

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,将大的数中,减去若干b的倍数,最终有一个数为0的话就胜了。
题解:假设a>=b,那么如果a==b,先手必胜,如果a%b==0,先手必胜。因为如果b<a<2*b的话,怎么取就已经定了,所以如果a>2*b,那么先手可以决定谁先取到b<a<2*b这个状态,所以如果a>2*b,先手必胜,只用讨论当b<a<2*b时最后谁胜。
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int n,m;
 5 int main()
 6 {
 7     while(~scanf("%d%d",&n,&m))
 8     {
 9         if(n+m==0)
10             break;
11         if(n<m)
12             swap(n,m);
13         if(n%m==0)
14         {
15             printf("Stan wins\n");
16             continue;
17         }
18         bool flag=true;
19         while(1)
20         {
21             if(m==0||n%m==0||n/m>=2)
22                 break;
23             int t=n;
24             n=m;
25             m=t-n;
26             flag=!flag;
27 
28         }
29         if(flag)
30             printf("Stan wins\n");
31         else
32             printf("Ollie wins\n");
33     }
34 }

 

posted @ 2018-09-25 17:11  *starry*  阅读(408)  评论(0编辑  收藏  举报