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
------------------------------------------------------------------
题意:两个人在玩一个类似gcd的游戏,从两个数中不断用较大数中减去较小数的倍数,谁先得到0谁获胜,问谁赢。
分析:一开始还以为是要跑gcd,然而还是太naive了。看了一下《挑战》,总结了一下思路。
  {策略1}:对于(a,b)(a>b)来说,如果a是b的倍数,那么肯定当前者赢。
  {策略2}:对于(a,b)(a>b)来说,如果a<2*b,那么意味着下一个状态必定是(b,a-b),因为此时a不可能减去更多倍的b,以此类推之后,永远不会出现a%b==0的情况,因为如果能出现的话,策略1就会将其判定了。那么肯定当前者赢。
  用上策略1和策略2就AC了。
 #include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
long long a,b;//f=1表示stan赢
while( scanf("%lld%lld",&a,&b)== && a!= && b!= )
{
int f=;
while()
{
if(a>b) swap(a,b);
if(b%a==) break;//策略1
if(b-a>a) break;//策略2
b-=a;
f*=-;
}
if(f==) printf("Stan wins\n");
else printf("Ollie wins\n");
}
return ;
}

【POJ】2348 Euclid's Game(扩欧)的更多相关文章

  1. poj 2348 Euclid's Game 题解

    Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9023   Accepted: 3691 Des ...

  2. POJ 2348 Euclid's Game(辗转相除博弈+自由度分析)

    题目链接:http://poj.org/problem?id=2348 题目大意:给你两个数a,b,Stan和Ollie轮流操作,每次可以将较大的数减去较小的数的整数倍,相减后结果不能小于0,谁先将其 ...

  3. POJ 2348 Euclid's Game 博弈论

    http://poj.org/problem?id=2348 顺便说,必应翻译真的好用,比谷歌翻译好用100倍. 很难判断这道题的具体博弈类型. 有两种写法,一种是找规律,一种是推理得到关系后循环(或 ...

  4. POJ 2348 Euclid's Game(博弈论)

    [题目链接] http://poj.org/problem?id=2348 [题目大意] 给出两个数,两个参赛者轮流用一个数减去另一个数的倍数,当一个数为0的时候游戏获胜, 求先手是否必胜 [题解] ...

  5. POJ 2348 Euclid's Game【博弈】

    题目链接: http://poj.org/problem?id=2348 题意: 给定两个数,两个人每次从较大数中减去较小数的倍数,谁先得到0谁获胜,为谁赢? 分析: 令一种可能出现的整数对为(a,b ...

  6. POJ 2348 Euclid's Game(博弈)题解

    题意:有a,b两个数字,两人轮流操作,每次可以选择两个之中较小的数字,然后另一个数字减去选择数字的任意倍数(不能减到负数),直到其中一个为0,不能操作为败 思路:这题用博弈NP思想,必败点和必胜点之间 ...

  7. POJ 2348 Euclid Game (模拟题)

    Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7942   Accepted: 3227 Des ...

  8. POJ 2348 Euclid's Game(简单博弈)

    这道题没说a b最大多少,所以要声明为long long型,不然会WA! 道理很简单,(默认a>=b)a和b只有以下三种关系: 1.a%b==0 :这种关系下,可能是a/b为整数,也可能是a和b ...

  9. poj 2348 Euclid's Game

    题目: 给两个整数a和b,两个人先后用较大的数减去较小数的整数倍,并且保证相减后为非负数.先把一个数变为0的人获胜. 分析: 很显然,当大数是小数的整数倍时为必胜态. 从这道题学会一个叫做自由度的东西 ...

  10. POJ 2348 Euclid's Game (博弈)

    题意:给定两个整数,两个人轮流操作,每次可以用较大数减去较小数的整数倍,当一个数变成0时,则结束,问谁会胜. 析:很明显如果 a == b 那么就可以直接结束了,那么如果 a > b我们可以交换 ...

随机推荐

  1. 网络唤醒原理浅析(Wake On LAN)

    之前我的一篇文章<网络唤醒全攻略(Wake On Lan)>介绍过如何设置远程唤醒电脑,着重于使用,这篇主要从原理方面解析一下当中的奥妙: 原理 将唤醒魔术包发送的被唤醒机器的网卡上,魔术 ...

  2. 在Intellij IDEA中使用Debug

    Debug用来追踪代码的运行流程,通常在程序运行过程中出现异常,启用Debug模式可以分析定位异常发生的位置,以及在运行过程中参数的变化.通常我们也可以启用Debug模式来跟踪代码的运行流程去学习三方 ...

  3. 泛型集合转化为DataTable

    public class DataTableUtil { /// <summary> /// 泛型集合转化为dataTable /// </summary> /// <t ...

  4. Centos 7服务启动文件

    在Centos 7中,如果要编辑一个脚本服务文件,并使用systemd进行管理,则必须将服务文件命名为/etc/systemd/system/*.service. service unit文件中的选项 ...

  5. 使用反射+策略模式代替项目中大量的switch case判断

    我这里的业务场景是根据消息类型将离线消息存入mongoDB不同的collection中.其中就涉及到大量的分支判断,为了增强代码的可读性和可维护性,对之前的代码进行了重构. 先对比一下使用反射+策略模 ...

  6. 花生壳DDNS为何不支持LetsEncrypt申请

    Inspired by Let's Encrypt Community , thx to sahsanu, jsha, and orzorc. 开端 Lets Encrypt 是一款免费的网站 SSL ...

  7. Rational Rose2013安装及破解教程

    1.下载地址:http://pan.baidu.com/s/1mhKGfHY 2.安装:解压缩文件"[Rational.Rose.Enterprise.Edition.2003].Softw ...

  8. windows 下 Mutex和Critical Section 区别和使用

    Mutex和Critical Section都是主要用于限制多线程(Multithread)对全局或共享的变量.对象或内存空间的访问.下面是其主要的异同点(不同的地方用黑色表示). Mutex Cri ...

  9. Java学习10——package和import

    package和import语句 为了便于管理大型软件系统中数目众多的类,解决类的命名冲突问题,Java引入包(package)机制,提供类的多重类命名空间,使用时,import引入相应package ...

  10. Java学习2——HelloWorld(编写第一个java程序)

    编写 在自己的工作文件目录下(如上一篇中配置的classpath路径)创建HelloWorld.java文件,编写如下代码,并保存 public class HelloWorld { public s ...