[简单博弈] hdu 1525 Euclid'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 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

Recommend

LL   |   We have carefully selected several similar problems for you:  1536 1524 1404 1527 1846

题目意思:

两个人,两堆石头,玩游戏。每次从石头数多的那堆中拿走石头数少的那堆石头的个数的倍数个,保证拿了过后不能为负数。谁恰好使其中一堆石头数为0,谁使其中某一堆石头为0,谁赢。两人都足够聪明。

解题思路:

借助欧几里得算法的博弈。

显然如果a>b 且a>2*b  先拿者必赢 因为他可以决定是自己还是对手遇到状态(a%b,b)如果状态(a%b,b)为必输状态,他就让对手面对(拿走a-a%b个)。如果为必赢状态,他就自己面对(拿走-a%b-b个).

如果b<a<=2*b 只能到达(a%b,b)的状态。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

int gcd(ll a,ll b)
{
    if(a<b) //求出谁大
        swap(a,b);
    if(b==0) //终止 必输状态
        return 0;
    if(a>2*b) //必胜
        return 1;

    return gcd(b,a%b)^1; //和拿了之后的输赢情况相反
}

int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   ll a,b;

   while(scanf("%I64d%I64d",&a,&b))
   {
       if(!a&&!b)
          break;
       int ans=gcd(a,b);

       if(ans)
            printf("Stan wins\n");
       else
            printf("Ollie wins\n");
   }
   return 0;
}

[简单博弈] hdu 1525 Euclid's Game,布布扣,bubuko.com

[简单博弈] hdu 1525 Euclid's Game

时间: 2024-10-25 20:10:58

[简单博弈] 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

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,

HDU 1846 Brave Game (简单博弈)

HDU 1846 Brave Game (简单博弈) ACM 题目地址: HDU 1846 Brave Game 题意: 中文. 分析: 博弈入门. 如果n=m+1,因为最多取m个,所以先拿的人拿多少个,后拿的人能全拿走. 所以判断n%(m+1)即可. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1846.cpp * Create Date: 2014-09-20 10:05:26 * Descripton: game */ #

hdu 2999 sg函数(简单博弈)

Stone Game, Why are you always there? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 393    Accepted Submission(s): 132 Problem Description "Alice and Bob are playing stone game...""E

【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

ACM: NBUT 1107 盒子游戏 - 简单博弈

NBUT 1107  盒子游戏 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format: Practice Appoint description:  System Crawler  (Aug 13, 2016 10:35:29 PM) Description 有两个相同的盒子,其中一个装了n个球,另一个装了一个球.Alice和Bob发明了一个游戏,规则如下:Alice和Bob轮流操作,Alice先操作每次操作时,游戏者先看看

[Nim博弈]hdu 1850 Being a Good Boy in Spring Festival

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1850 Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4053    Accepted Submission(s): 2394 Problem Description