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

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 }

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

 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 }

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

时间: 2024-10-21 13:07:37

hdu------(1525)Euclid's Game(博弈决策树)的相关文章

hdu 1525 Euclid&#39;s Game 博弈

Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2219    Accepted Submission(s): 1000 Problem Description Two players, Stan and Ollie, play, starting with two natural numbers. Stan

[简单博弈] hdu 1525 Euclid&#39;s Game

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1525 Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1832    Accepted Submission(s): 808 Problem Description Two players, Stan and

hdu 1525 Euclid&#39;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,

HDU 1525 (博弈) Euclid&#39;s Game

感觉这道题用PN大法好像不顶用了,可耻地看了题解. 考虑一下简单的必胜状态,某一个数是另一个数的倍数的时候是必胜状态. 从这个角度考虑一下:游戏进行了奇数步还是偶数步决定了哪一方赢. 如果b > 2a,那么这一方就有权利改变游戏步数的奇偶性,从而到达对自己有利的状态,所以这是一个必胜状态. 如果a < b < 2a,那么下一步只能到达(b-a, a)状态,一直模拟就行. 1 #include <cstdio> 2 #include <algorithm> 3 us

HDU 1525 博弈

Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1880    Accepted Submission(s): 825 Problem Description Two players, Stan and Ollie, play, starting with two natural numbers. Stan,

【HDOJ】1525 Euclid&#39;s Game

自己想明白的第一道博弈.首先a==b的时候肯定是先手赢: 然后当a>=2*b时,不妨假设a=nb+k, k<b,因此,不论后续怎么博弈,一定可以出现a=k, b=b的情况.因此,无论这个局面是胜或负,先手者一定可以得到利于自己的局面.若(k,b)为负,则先手者从a减去nb,则先手胜:若(k,b)为胜,先手者从a减去(n-1)*b,则先手仍然胜. 当b<a<2*b时,只能对a减去b,然后进入下一轮仍旧按照上述策略博弈. 1 /* 1525 */ 2 #include <cstd

HDU 2147 kiki&#39;s game(博弈图上找规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2147 题目大意:给你一个n*m的棋盘,初始位置为(1,m),两人轮流操作,每次只能向下,左,左下这三个方向移动,谁最后无法移动棋子就输掉比赛,问先手是否会获胜. 解题思路:简单题,P/N分析找规律,以(n,m)点为结束点推到起始点,如图: 发现每个田字格的状态都是一样的,因为(n,m)点一定时P态,所以可以得出规律:只有当(m%2==1&&n%2==1)时,先手才会输. 代码: 1 #incl

hdu 2147 kiki&#39;s game 博弈

点击打开链接链接 kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others) Total Submission(s): 6933    Accepted Submission(s): 4141 Problem Description Recently kiki has nothing to do. While she is bored, an idea appears

POJ 2348 Euclid&#39;s Game(博弈)

题目地址:POJ 2348 每一步只有如下三种情况:(假设a>=b) 1:a%b==0    这时候自然是必败态. 2:a<2*b  这时候的下一步是别无选择的,只能是变为(a-b,a),由于该步是唯一的,所以必胜态与必败态是交替的. 3:a>2*b  这时候是必胜态.为什么呢?因为此时总可以转移到一个必败态.由于第2情况的时候两种状态是交替的,而这时候由总可以转换成(a,a%b)和(a,a%b+b),而(a,a%b+b)与(a,a%b)又属于第2种情况的相邻的,所以必有一个是必败态.根