码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1525(Euclid's Game)规律博弈

时间:2019-09-10 18:03:29      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:ali   分析   tin   one   swa   number   gif   roc   des   

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是大的那个),a可以减去b的若干倍,完成这一步操作后,数字中有零的人获胜。没有则使用新的a和b继续游戏。
 
分析:不管怎么采取决策最终都会有(b,a%b)的局面出现,如果a∈[b, 2*b],那么接下来出现的情况只能是[b,a-b];否则有多种情况。
 

a / b == 1   即a ∈[b , 2b)  , 下一步 只能到(b , a  - b)

a / b >= 2 时, 先手可以选择让谁来面对 (b ,  a%b)这个局势,  显然先手足够聪明, (b , a % b) 这个局势谁胜谁负 已经清楚, 所以先手必胜

假设(a%b)/b>=2,导致后手可以选择对自己有利的局势,那先手就可以把当前局势变为(a%b+b,b),让后手被迫选择对自己有利的局势。同理,如果下面好几个局势都是‘a‘>2*‘b‘的,第一个面对a>=2*b的局势的聪明人早就想好了怎么走。。

 

技术图片
#include<stdio.h>
#include<algorithm>
using namespace std;
int a,b;
int main()
{
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        if(a==0&&b==0)
            break;
        if(a<b)
        swap(a,b);
        int flag=0;
        while(b)
        {
            if(a%b==0||a/b>=2)
            break;
            a-=b;
            swap(a,b);
            flag=1-flag;
        }
        if(flag)
            printf("Ollie wins\n");
        else
            printf("Stan wins\n");
    }
}
View Code

 

 

HDU1525(Euclid's Game)规律博弈

标签:ali   分析   tin   one   swa   number   gif   roc   des   

原文地址:https://www.cnblogs.com/switch-waht/p/11498442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!