【博弈论】Euclid's Game

题目描述:

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.

输入

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.

输出

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.

样例输入

34 12
15 24
0 0

样例输出

Stan wins
Ollie wins

分析:设a>b,若a%b=0,先手必赢。若a>2*b,可以知道a%b,b是必胜态还是必输态,那么先手就可以决定谁先到达a%b,b这个状态,所以先手必赢。当b<a<2*b时,则需一步步走到0为止。

#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int a,b;
    while(scanf("%d%d",&a,&b) == 2)
    {
        if( a == 0 && b == 0)break;
        if( a < b)swap(a,b);
        int win = 0;
        while(b)
        {
            if( a%b == 0 || a/b >= 2)break;
            a = a-b;
            swap(a,b);
            win ^= 1;
        }
        if(win == 0)printf("Stan wins\n");
        else printf("Ollie wins\n");
    }
    return 0;
}

【博弈论】Euclid's Game

原文地址:https://www.cnblogs.com/xyfs99/p/11722093.html

时间: 2024-11-08 21:33:42

【博弈论】Euclid's Game的相关文章

[poj2348]Euclid&#39;s Game(博弈论+gcd)

Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9033   Accepted: 3695 Description Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser o

【博弈论】poj2348 Euclid&#39;s Game

假设当前b>a. 一.b%a==0 必胜 二.b<2*a,当前我们没有选择的余地,若下一步是必胜(最终能到情况一),则当前必败:反之,当前必胜. 三.b>2*a,假设x是使得b-ax<a的整数,考虑一下从b中减去a(x-1)的情况,例如对于(4,19)则减去12. 此时,接下来的状态就成了前边讲过的没有选择余地的情况二,若该状态是必败态的话,当前状态就是必胜态. 若该状态是必胜态的话,其下一步是唯一确定的,因此是必败态,所以我们可以直接到达此态. ∴情况三是必胜态. ∴先达到情况一

POJ - 2348 Euclid&#39;s Game(博弈论入门题)

题目链接:poj.org/problem?id=2348 题意:给出两个数,两个人进行博弈,每个人都采取最优策略. 每个人可以进行操作:两个数中较大数减去较小数的倍数(可以是1,2...X倍),使得其中一个数先为零的获胜. 每次都先把较小值给a,较大值给b.一开始把必胜态给先手的那个人,然后进行判断. 1.b-a<=a  没办法只能一次一次计算,必胜态不断变换,直到其中一个数刚好为0. 2.b-a>a   不管怎样都是必胜态.b - xa <= a如果这个是必败态,那么b - (x-1)

ZOJ 1913 Euclid&#39;s Game 博弈论

题目描述 小明和小红在玩欧几里得游戏.他们从两个自然数开始,第一个玩家小明,从两个数的较大数中减去较小数的尽可能大的正整数倍,只要差为非负即可.然后,第二个玩家小红,对得到的两个数进行同样的操作,然后又是小明.就这样轮流进行游戏,直至某个玩家将较大数减去较小数的某个倍数之后差为0为止,此时游戏结束,该玩家就是胜利者. 输入格式 输入包含多组测试数据.每组输入两个正整数,表示游戏一开始的两个数,游戏总是小明先开始. 当输入两个0的时候,输入结束. 输出 对于每组输入,输出最后的胜者,我们认为他们两

POJ【数论/组合/博弈论】

 POJ[数论/组合/博弈论]题目列表 POJ[数论/组合/博弈论]题目列表 原来的列表比较水,今天换了一个难一些的列表,重新开始做~ 红色的代表已经AC过,蓝色的代表做了但是还没过.这句话貌似在我空间里的每份列表里都有额. 博弈论 POJ 2234 Matches Game POJ 2975 Nim POJ 2505 A multiplication game POJ 1067 取石子游戏 POJ 2484 A Funny Game POJ 2425 A Chess Game POJ 29

博弈论类题目小结——转载

出处http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 首先当然要献上一些非常好的学习资料: 基础博弈的小结:http://blog.csdn.net/acm_cxlove/article/details/7854530 经典翻硬币游戏小结:http://blog.csdn.net/acm_cxlove/article/details/7854534 经典的删边游戏小结:http://blog.csdn.net/acm

POJ 2348-Euclid&#39;s Game(博弈论)

Euclid's Game Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2348 Appoint description:  System Crawler  (2015-03-11) Description Two players, Stan and Ollie, play, starting with two natural num

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now

POJ 2348 Euclid&#39;s Game

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