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

题意:有a,b两个数字,两人轮流操作,每次可以选择两个之中较小的数字,然后另一个数字减去选择数字的任意倍数(不能减到负数),直到其中一个为0,不能操作为败

思路:这题用博弈NP思想,必败点和必胜点之间的转化。

我们假设当前状态为(x,y)其中x>=y,那么必有以下任一状态:

1. x < 2 * y:那么这是只能执行一个操作x - y,如果这个操作之后变成必败态,那么当前为必胜态;反之亦然。

2. x >= 2 * y:显然此时有多种选择,假设x = k * y,如果x - (k - 1) * y就变成了第一种情况,如果x - k  * y就变成了第一种情况操作后的情况,显然这其中有一个是必胜态,那么此时情况2必然是必胜情况。

参考:Euclid‘s Game(poj2348+博弈)

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = 1e6 + 10;
const int seed = 131;
const ll MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;

int main(){
    ll a, b;
    while(~scanf("%lld%lld", &a, &b) && a + b){
        int times = 0, num = 0;
        while(a != 0 && b != 0){
            times++;
            if(a > b) swap(a, b);
            num = b / a;
            if(num >= 2) break;
            b -= a;
        }
        if(times & 1) printf("Stan wins\n");
        else printf("Ollie wins\n");
    }
    return 0;
}

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

原文地址:https://www.cnblogs.com/KirinSB/p/9697250.html

时间: 2024-07-30 12:22:07

POJ 2348 Euclid's Game(博弈)题解的相关文章

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

题目地址:POJ 2348 每一步只有如下三种情况:(假设a>=b) 1:a%b==0    这时候自然是必败态. 2:a<2*b  这时候的下一步是别无选择的,只能是变为(a-b,a),由于该步是唯一的,所以必胜态与必败态是交替的. 3:a>2*b  这时候是必胜态.为什么呢?因为此时总可以转移到一个必败态.由于第2情况的时候两种状态是交替的,而这时候由总可以转换成(a,a%b)和(a,a%b+b),而(a,a%b+b)与(a,a%b)又属于第2种情况的相邻的,所以必有一个是必败态.根

POJ 2348 Euclid&#39;s Game(辗转相除博弈+自由度分析)

题目链接:http://poj.org/problem?id=2348 题目大意:给你两个数a,b,Stan和Ollie轮流操作,每次可以将较大的数减去较小的数的整数倍,相减后结果不能小于0,谁先将其中一个数字变成0谁就获胜. 解题思路:看了挑战程序设计上的,这里我们先假设a<b,当b是a的整数倍是必胜态.我们讨论以下b不是a的整数倍,此时a,b的关系按照自由度的观点(第一次听说),可以分为以下两种情况: ①b-a<a ②b-a>a 那么对于①,玩家只有从b中减去a这一个选择.如果b-a

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)

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

POJ 2348 Euclid&#39;s Game 组合游戏

题目大意:有两个人玩游戏,有两堆石子,每次一个人要从其中一堆石子中拿走一些石子,当出现有一对石子变成0的时候这个人就输了,另一个人就赢了.给出初始石子有多少,问谁能赢. 思路:基础的组合游戏的判定问题,这个题没有给数据范围,非常的坑爹,据说需要long long. 第一次做组合游戏的题目,想想还有些小激动呢.昨天听同学讲了讲,我来现学现卖一下: 由于组合游戏的公平性,那么:如果一个状态的子状态会使先手必败,那么当前状态的先手必胜: 如果不存在一个子状态会使先手必败,那么当前状态先手必败. 利用这

POJ 2348 Euclid Game (模拟题)

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

【POJ】2348 Euclid&#39;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

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

POJ 2406 Power Strings KMP运用题解

本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都觉得是可以利用next数组的,但是自己想了很久没能这么简洁地总结出来,也只能查查他人代码才恍然大悟,原来可以这么简单地区求一个周期字符串的最小周期的. 有某些大牛建议说不应该参考代码或者解题报告,但是这些大牛却没有给出更加有效的学习方法,比如不懂KMP,难倒不应该去看?要自己想出KMP来吗?我看不太可能有哪位大牛可以直接自己"重新创造出KMP"来吧. 好吧,不说"创造KMP