POJ2348 Euclid's Game

原题链接:http://poj.org/problem?id=2348

Euclid’s Game

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

题解

我们可以把这个过程理解为一个多阶段 Nim N i m ,有多堆石子,每堆石子都有一个可以取的次数,每次至少取一次,可以取多次,在取完上一堆后才能取下一堆,以样例中的第一组数据为例:

(34,12)可以取两个12,取完以后变成(12,10)
(12,10)只能取一次,变成(10,2)
(10,2)取5次取完变成(2,0)

那么转化为多阶段 Nim N i m 游戏是3堆石子:2,1,5

在多阶段 Nim N i m 中,如果先手取下一堆能够赢,那么在取这一对的时候就可以将这堆石子取的刚好剩下一个,强迫对手取完这一堆;反之,就可以直接将这堆石子取完,让对手去取下一堆。只有当某堆石子只能取一次的时候,先手的人别无选择必须取完。

那么问题就变得简单起来,只要谁掌握了先手(即第一个遇到可取次数大于1的情况),谁就能获胜。

代码
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
bool check(int x,int y,bool p)
{
    if(x%y==0)return p;
    if(x/y>1)return p;
    return check(y,x%y,!p);
}
void ac()
{
    if(n<m)swap(n,m);
    if(check(n,m,1))printf("Stan wins\n");
    else printf("Ollie wins\n");
}
int main()
{
    while(scanf("%d%d",&n,&m)&&n&&m)ac();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值