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): 2836 Accepted Submission(s): 1296

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

题意:两个人玩游戏,有两个数,每次可以选择用较大的数减去较小的数的倍数,谁使其出现0谁赢

思路:可以得出,两个人必然会有面对 b,a 这种局面的,其中 b 为较大数,a为较小数, b/a<2 ,然后他们只有一次一次的减,此时,可以判断奇偶来得出胜负,然后分析初始局面如果是 b/a>=2 ,当面临 b/a<2 的局面注定失败的时候,先手可以选择直接选择使后手面临此局面,当面临 b/a<2 的局面注定胜利的时候,先手选择使后手面临 (b+a)/a=2 的局面,那么后手只能选择让先手面临 b/a<2 的局面,所以谁面临 b/a>=2 谁赢,而且谁面临 b%a=0 的时候直接减完就赢了.

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-07-29-16.42 Friday
File Name    : D:\MyCode\2016-7月\2016-7-29.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF,n||m)
    {
        if(n>m) swap(n,m);
        int cnt=0,a=n,b=m;
        while(a)
        {
            if(b%a==0||b/a>=2) break;
            int t=a;a=b-a;b=t;
            cnt++;
        }
        if(cnt%2==0)
            printf("Stan wins\n");
        else
            printf("Ollie wins\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值