Euclid's Game
Time Limit: 1 Second Memory Limit: 32768 KB
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
题目描述
欧几里得游戏:2个玩家,Stan和Ollie,以2个自然数开始。第一个玩家,Stan,从2个数中较大数中减去较小数的任何正整数倍,只要差为非负即可。然后,Ollie,对剩下的2个数进行同样的操作,然后Stan,等等。直至较大数减去较小数的某个倍数之后差为0为止。
欧几里得游戏(Euclid's Game)
题意很简单,两个数,只能由大数减去小数的整数倍,最先减出0的获胜。不需要用SG什么的,直接模拟就可以。看题解发现一个技巧:a,b(a>b)两个数,如果a>2*b,那么(a,b)可以选择到达(a%b,b) or (a%b+b,b)两种状态,而两种状态所处的阶段是相反的,所以如果是理想化的完美操作的化,这一定是一个必胜状态。只需考虑a<2*b时就可以。
用递归来简化思维。
#include <bits\stdc++.h>
using namespace std;
bool game(int a,int b)
{
if(a<b) swap(a,b);
if(b==0||a%b==0) return 1;
if(a>2*b) return 1;
else
{
return !game(a-b,b);
}
}
int main()
{
int a,b,x,y,t,cont;
while(scanf("%d%d",&a,&b),a&&b)
{
if(game(a,b)) printf("Stan wins\n");
else printf("Ollie wins\n");
}
return 0;
}