poj 2348 Euclid's Game 题解
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 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 |
[Submit] [Go Back] [Status] [Discuss]
————————————————————————我是分割线——————————————————————————
博弈论题目。
第一眼看见我竟以为是nim游戏 TAT~
其实当n/m>=2时即为关键点,此时关键点掌控方可以决定游戏胜败。
所以判断谁掌握关键点即可。
1 /* 2 Problem: 3 OJ: 4 User: S.B.S. 5 Time: 6 Memory: 7 Length: 8 */ 9 #include<iostream> 10 #include<cstdio> 11 #include<cstring> 12 #include<cmath> 13 #include<algorithm> 14 #include<queue> 15 #include<cstdlib> 16 #include<iomanip> 17 #include<cassert> 18 #include<climits> 19 #include<functional> 20 #include<bitset> 21 #include<vector> 22 #include<list> 23 #include<map> 24 #define F(i,j,k) for(int i=j;i<=k;i++) 25 #define M(a,b) memset(a,b,sizeof(a)) 26 #define FF(i,j,k) for(int i=j;i>=k;i--) 27 #define maxn 10001 28 #define inf 0x3f3f3f3f 29 #define maxm 1001 30 #define mod 998244353 31 //#define LOCAL 32 using namespace std; 33 int read(){ 34 int x=0,f=1;char ch=getchar(); 35 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 36 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 37 return x*f; 38 } 39 int n,m; 40 inline int gcd(int a,int b) 41 { 42 return b ? gcd(b,a%b) : a; 43 } 44 int main() 45 { 46 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 47 #ifdef LOCAL 48 freopen("coin.in","r",stdin); 49 freopen("coin.out","w",stdout); 50 #endif 51 while(cin>>n>>m&&(n!=0||m!=0)) 52 { 53 if(n<m) swap(n,m); 54 if(m==0) cout<<"Ollie wins"<<endl; 55 else if(n%m==0) cout<<"Stan wins"<<endl; 56 else{ 57 long long cnt=0; 58 while(m!=0&&n/m==1){ 59 cnt++; 60 n=n-m;swap(n,m); 61 } 62 // cout<<cnt<<endl; 63 if(cnt%2==0) cout<<"Stan wins"<<endl; 64 else cout<<"Ollie wins"<<endl; 65 } 66 } 67 return 0; 68 }