hdu 1525 Euclid's Game 博弈

来源:互联网 发布:json.stringify()使用 编辑:程序博客网 时间:2024/05/29 17:40

Euclid's Game

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


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
 题意: 给你两个数 每次大的数可以减掉小的数的整数倍 知道一个人面临其中一个数为0 的情况那么则为输。。
思路: 比赛的时候一直想枚举某一个状态的后继 如果他的后继中有一个为输的状态 那么他一定为赢的状态 如果一个状态的后继全部是赢的状态 那么他一定是输的状态 ,但是对比较大的数 那么枚举就会很困难。。
在这里我们注意到 两个数 max 和 min 能转移到的状态 ,
如果max%min ==0 那么一定为赢的状态
如果 min ==0 那么一定为输的状态
然后对于max/min>=2 的状态 他一定一定一定会转移到 min max%min 的状态 因为min max%min 的状态是可以确定为赢还是输的状态 而对于先手就可以决定是将 min max%min 状态留给自己还是对手 。
对于max/min>=1 且小于2的情况那么就只有一种转移情况了 就只能转移到 max-min min 的状态。。
代码:
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;int maxx,minn;int main(){    int t1,t2;    while(cin>>t1>>t2)    {        if(t1==0&&t2==0) break;        maxx=max(t1,t2);        minn=min(t1,t2);        int cnt=1;        while(1)        {            if(minn==0||maxx%minn==0||maxx/minn>=2) break;            cnt++;            //printf("%d %d\n",minn,maxx);            t1=minn;            t2=maxx-minn;            maxx=max(t1,t2);            minn=min(t1,t2);        }        if(cnt%2) printf("Stan wins\n");        else printf("Ollie wins\n");    }    return 0;}


原创粉丝点击