• 原题如下:

    Euclid's Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10832   Accepted: 4426

    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<b,另外,如果b已经是a的倍数了则必胜,所以假设b并非a的倍数。
    此时,a和b的关系,按自由度的观点,可以分成一下两类。
    ① b-a<a的情况
    ② b-a>a的情况
    对于第一种情况,只能从b中减去a,而第二种情况则有多种选择。
    对于第一种情况,判断必胜还是必败比较简单,如果b减去a之后所得到的状态是必败态的话,它就是必胜态,如果得到的是必胜态的话它就是必败态。
    对于第二种情况,假设x是使得b-ax<a的最小整数,考虑b-a(x-1)的情况。此时,接下来的状态就成了前边讲过的没有选择余地的第一种情况。如果该状态是必败态的话,当前状态就是必胜态。那么,如果减去a(x-1)后的状态是必胜态的话,怎么办?此时,b-ax的状态是b-a(x-1)后的状态唯一可以转移到的状态,根据假设,b-a(x-1)后的状态是必胜态,所以b-ax后的状态是必败态,那么当前状态就是必胜态。由此可知,第二种情况总是必胜的。所以,从初状态开始,最先达到有自由度的第二种状态的一方必胜。
  • 代码:
    #include<cstdio>
    #include<algorithm> using namespace std; void swap(int &a, int &b)
    {
    int t=a;
    a=b;
    b=t;
    } int main()
    {
    int a, b;
    while (scanf("%d %d", &a, &b))
    {
    if (a== && b==) break;
    bool f=true;
    for (;;)
    {
    if (a>b) swap(a, b);
    if (b%a==) break;
    if (b-a>a) break;
    b-=a;
    f=!f;
    }
    if (f) puts("Stan wins");
    else puts("Ollie wins");
    }
    }

Euclid's Game(POJ 2348)的更多相关文章

  1. 4.1.3 Euclid's Game (POJ 2348)

    Problem description: 以辗转相除法为基础,给定两个整数a和b,Stan和Ollie轮流从较大的数字中减去较小数字的倍数(整倍数),并且相减后的结果不能为零.Stan先手,在自己的回 ...

  2. poj 2348 Euclid's Game 题解

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

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

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

  4. POJ 2348 Euclid's Game 博弈论

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

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

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

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

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

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

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

  8. POJ 2348 Euclid Game (模拟题)

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

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

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

随机推荐

  1. ElementUI——级联和树形省市区

    项目中有用到省市区选择,我们是三个接口获取数据的,在此记录一下级联和树形的区别: 级联HTML: <el-cascader :props="region"></e ...

  2. javaSE总结(转+总结)

    一:java概述: 1,JDK:Java Development Kit,java的开发和运行环境,java的开发工具和jre. 2,JRE:Java Runtime Environment,java ...

  3. Eclipse怎么切换工作空间

    1.进行点击Eclipse编辑代码的窗口界面中,进行点击菜单中file的选项. 2.弹出了下拉菜单中进行选择为“switch workspace”的选项. 3.弹出了下一级菜单中进行选择为other的 ...

  4. firewall几个常用命令

    systemctl status firewalld firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --r ...

  5. 仿京东BOE官网 jQuery代码

    $(function() { $("#chanping").mouseenter(function() { $("#column").slideDown(500 ...

  6. 团队作业3:需求改进&系统设计

    一.需求&原型改进: 1.1 选题需求改进 用户:同学你好,有什么事我们能帮到你 我:我要说的事……(咽口水)你们千万别害怕 用户:我们是杠精,我们不会怕,您请说 我:我们要做一个roguel ...

  7. 23种设计模式(C++)

    每一种都有对应理解的相关代码示例 → Git原码 一. GOF-23 模式分类 从目的来看 • 创建型(Creational)模式:将对象的部分创建工作延迟到子类或者其他对象,从而应对需求变化为对象创 ...

  8. [ASP.NET Core开发实战]开篇词

    前言 本系列课程文章主要是学习官方文档,再输出自己学习心得,希望对你有所帮助. 课程大纲 本系列课程主要分为三个部分:基础篇.实战篇和部署篇. 希望通过本系列课程,能让大家初步掌握使用ASP.NET ...

  9. mq本机上能用,其他机子上连上不mq

    有一次遇到了记录下.原因是suse11重启后会自动开防火墙. 关了防火墙. rcSuSEfirewall2 stopShutting down the Firewall done

  10. 善用Bash history 命令

    大家好,我是良许 相信大家平时都有用 history 命令来查看命令历史记录,但是实际上 history 命令并非只有这个功能,history 还有很多有用的功能.尤其是 Bash 版本的 histo ...