hdu 1525 Euclid's Game

Euclid's Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2074    Accepted Submission(s): 924


Problem 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
如果 a,b之间是倍数关系,那么第一个一定赢
假设 a<b,经过一系列的操作后,(a,b)一定会变成 (a,b-b/a*a)       //b/a是整除
如果b/a=1,就没有主动权,因为只能选择减掉一个a
如果b/a>1, 就掌握了主动权:可以之间选择减掉b/a*a,让后面的人面对(a,b-b/a*a),也可以减掉(b/a-1)*a,后面的人就必须减掉一个a,让自己面对(a,b-b/a*a);
所以只要看谁先掌握主动权,谁就能赢(如果有主动权的话,即b/a>1)
 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<queue>
 6 #include<stack>
 7 #include<algorithm>
 8 #include<cstring>
 9 #include<stdlib.h>
10 using namespace std;
11 int p[101000],cnt;
12 int main(){
13     int n,m;
14     while(cin>>n>>m,n+m){
15         if(n>m) swap(n,m);
16         if(m%n==0){
17             cout<<"Stan wins"<<endl;continue;
18         }
19         cnt=0;
20         while(m%n!=0){
21             p[cnt++]=m/n;
22             m-=m/n*n;
23             if(n>m) swap(n,m);
24         }
25         int can=0,ps=-1;
26         for(int i=0;i<cnt;i++)
27           if(p[i]>1){
28             ps=i;break;
29           }
30         if(ps==-1) {
31             if(cnt%2) cout<<"Ollie wins"<<endl;
32             else cout<<"Stan wins"<<endl;
33         }
34         else{
35             if(ps%2) cout<<"Ollie wins"<<endl;  //第二个首先掌握主动权
36             else cout<<"Stan wins"<<endl;
37         }
38     }
39 }

 

 

posted on 2014-08-23 23:00  天凉了  阅读(135)  评论(0编辑  收藏  举报

导航