Euclid's Game
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 Source |
题意是给你两个整数a和b,Sten和Ollie轮流去两者数中大数减去小数的整倍数,结果不能小于零,Sten先手,在自己的回合得到0者获胜,问谁获胜。
思路:a和b之间的大小关系分以下三种。(假设a < b)
一:b是a的整倍数,必胜。
二:b - a < a
这种情况下只能用b减去a,没有选择,必胜态和必败态互相转换。
三:b - a > a
这里我们假设b - ax < a;我们来讨论一下减去a * (x - 1)的情况,如果减去以后是必败态,那么当前为必胜态。
如果减去之后为必胜态,我们知道b - ax的状态是b - a(x - 1)唯一可以转移到的状态,因此b - ax为必败态,当前为必胜态。
所以b - a > a为必胜。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 15 + 5;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-10
const long long MOD = 1000000000 + 7;
const int mod = 10007;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
typedef vector<int> vec;
typedef vector<vec> mat;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int main()
{
int a , b;
while(~scanf("%d%d" , &a , &b) , a||b)
{
int ok = 1;
while(1)
{
if(a > b)swap(a , b);
if(b % a == 0)break;
if(b - a > a)break;
b -= a;
ok ^= 1;
}
if(ok)puts("Stan wins");
else puts("Ollie wins");
}
return 0;
}