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,Stan和Ollie从两个数中较大的一个取另一个数的倍数,比如1倍,2倍,3倍……问谁能先把其中一个数取完。
分析:当b是a的倍数时,一定是必胜态;
当b-a>a时,如果取到b-x*a之后的状态是必败态,那么当前就是必胜态;如果取到b-x*a之后的状态是必胜态,那么我们只取到b-(x-1)*a,这样剩下的就满足b-a<a,只有一种取法,就是取到必胜态。所以不管怎么样 b-a>a时都是必胜态。
剩下的情况就很简单了,b-a<a,只有一种取法,就是从b中取出一个a。
代码如下:
#include<iostream>
using namespace std;
int main()
{
int a,b;
bool f;
while (cin>>a>>b){
if (a==0 && b==0) break;
f=1; //f==1代表先手赢
while (true){
if (a>b)
{
int tmp=b; b=a; a=tmp;
}
if (b%a==0) break; //b是a的倍数则是必胜态
if (b-a>a) break; //b-a>a则是必胜态
b=b-a;
f=!f;
}
if (f) cout<<"Stan wins\n";
else cout<<"Ollie wins\n";
}
return 0;
}