首页 > 代码库 > hdu------(1525)Euclid's Game(博弈决策树)

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 1215 240 0
 

 

Sample Output
Stan winsOllie wins
 

 

Source
University of Waterloo Local Contest 2002.09.28
 
题目的意思:
   有两个正整数数,a,b 轮流减去两个数min(a,b)中的倍数,谁最后得到o谁就won,注意两个人都可以对这两个数具有同样的操作......
代码:
     开始写一个决策树,不带路径压缩,然后玛德,无限tle...
    超时的代码:
   
 1 #include<cstring> 2 #include<cstdio> 3 bool flag=false; 4 int cont=1; 5 void botree(int n,int m){ 6     if(n<m)n^=m^=n^=m; 7     if(n%m) 8       for(int i=1;i*m<n;i++){ 9         cont++;10         botree(n-i*m,m);11      }12     else {13     if(cont&1) flag=true;14     cont=1;15     }16 }17 int main(){18     int n,m;19    //freopen("test.in","r",stdin);20  while(scanf("%d%d",&n,&m),n+m){21         flag=false;22         botree(n,m);23         if(flag) printf("Stan wins\n");24         else printf("Ollie wins\n");25  }26    return 0;27 }
View Code

 然后进行了优化之后.....得到这样的代码:

 
 1 /*Problem : 1525 ( Euclid‘s Game )     Judge Status : Accepted 2 RunId : 11528629    Language : C++    Author : huifeidmeng 3 Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta*/ 4  5 #include<cstring> 6 #include<cstdio> 7 bool flag=false; 8 int cont=1,cc=0; 9 void botree(int n,int m){10     if(n<m) n^=m^=n^=m;11     if(m>0&&((n%m)&&n/m==1)){12       cont++;13       botree(n%m,m);14      }15     else if(cont&1) flag=true;16 }17 int main(){18     int n,m;19   //freopen("test.in","r",stdin);20  while(scanf("%d%d",&n,&m),n+m){21         flag=false;22         cont=1;23         botree(n,m);24         if(flag) printf("Stan wins\n");25         else printf("Ollie wins\n");26  }27    return 0;28 }
View Code

 

hdu------(1525)Euclid's Game(博弈决策树)