博弈——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.
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
题目大意
两个人玩游戏,Stan 为先手,给定两个自然数,每次操作,都将大的数减去小的数的整数倍,先将其中一个数减为 0 的获胜。
分析
设有两个数 a,b(a >= b)
1.如果 a % b == 0,则先手获胜。
2. 如果 a >= 2 * b,那么先手可以根据 a%b,b 是必胜态还是必败态来进行接下去的操作。如果是必胜态,先手可以把 a,b 变为 a % b + b,b,如果是必败态,先手可以把 a,b 变成 a%b,b,即无论如何,先手都可以取得胜利。
3. 如果 b < a < 2 * b,这时,只能将 a,b 变为 a-b,b 这一种情况了,因此,在这种情况下,只能这样一步步模拟下去,直到出现 a % b == 0 || a >= 2 * b,然后根据模拟的次数判断谁输谁赢。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b&&a&&b){
if(a<b){
swap(a,b);
}
int cnt=0;
while(b){
if(a%b==0||a/b>=2){
break;
}
a=a-b;
swap(a,b);
cnt++;
}
if(cnt%2==0){
cout<<"Stan wins"<<endl;
}
else{
cout<<"Ollie wins"<<endl;
}
}
return 0;
}