Euclid's Game(POJ 2348)
- 原题如下:
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 0an 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 0Sample 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)的更多相关文章
- 4.1.3 Euclid's Game (POJ 2348)
Problem description: 以辗转相除法为基础,给定两个整数a和b,Stan和Ollie轮流从较大的数字中减去较小数字的倍数(整倍数),并且相减后的结果不能为零.Stan先手,在自己的回 ...
- poj 2348 Euclid's Game 题解
Euclid's Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9023 Accepted: 3691 Des ...
- POJ 2348 Euclid's Game(辗转相除博弈+自由度分析)
题目链接:http://poj.org/problem?id=2348 题目大意:给你两个数a,b,Stan和Ollie轮流操作,每次可以将较大的数减去较小的数的整数倍,相减后结果不能小于0,谁先将其 ...
- POJ 2348 Euclid's Game 博弈论
http://poj.org/problem?id=2348 顺便说,必应翻译真的好用,比谷歌翻译好用100倍. 很难判断这道题的具体博弈类型. 有两种写法,一种是找规律,一种是推理得到关系后循环(或 ...
- POJ 2348 Euclid's Game(博弈论)
[题目链接] http://poj.org/problem?id=2348 [题目大意] 给出两个数,两个参赛者轮流用一个数减去另一个数的倍数,当一个数为0的时候游戏获胜, 求先手是否必胜 [题解] ...
- POJ 2348 Euclid's Game【博弈】
题目链接: http://poj.org/problem?id=2348 题意: 给定两个数,两个人每次从较大数中减去较小数的倍数,谁先得到0谁获胜,为谁赢? 分析: 令一种可能出现的整数对为(a,b ...
- POJ 2348 Euclid's Game(博弈)题解
题意:有a,b两个数字,两人轮流操作,每次可以选择两个之中较小的数字,然后另一个数字减去选择数字的任意倍数(不能减到负数),直到其中一个为0,不能操作为败 思路:这题用博弈NP思想,必败点和必胜点之间 ...
- POJ 2348 Euclid Game (模拟题)
Euclid's Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7942 Accepted: 3227 Des ...
- POJ 2348 Euclid's Game(简单博弈)
这道题没说a b最大多少,所以要声明为long long型,不然会WA! 道理很简单,(默认a>=b)a和b只有以下三种关系: 1.a%b==0 :这种关系下,可能是a/b为整数,也可能是a和b ...
随机推荐
- 离线人脸识别门禁考勤——Android设备端APK及源码免费下载
适用场景:门禁场景的应用,适合安装在Android系统的门口机.闸机头.Pad等设备上. 主要功能:人员注册.人脸识别开门.考勤打卡.门禁权限管理.识别记录查询等. 预览效果: PC端 设备端1 设备 ...
- MySQL字符集操作
一.查看编码 show variables like 'character%'; 二.临时设置编码 1.set names xxx set names ${编码}; "set names x ...
- 在string.replace中使用具名组匹配
let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; let re = '2015-01-02'. r ...
- OVS 流表offload
原文链接:https://www.dazhuanlan.com/2019/12/31/5e0af1858dada/ 最近开始调研网卡的OVS流表offload功能,所以目前查看一下OVS这块是怎么做的 ...
- kernel 4.18.18 rpm 制作
1.下载kernel源码: wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.18.18.tar.gz 2.解压源码 ...
- First-Spike-Based Visual Categorization Using Reward-Modulated STDP
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Abstract 强化学习(RL)最近以击败欧洲围棋冠军等重大成就重新受到欢迎.在这里,我们第一次表明,RL可以有效地用于训练一个脉冲神经 ...
- 使用grub2引导进入Linux或Window系统
很多人在一通烂搞之后把自己的grub搞崩了(比如我当时手贱删除了boot分区)虽然后来又装了grub,但是进入grub后还是没有引导,只有一个孤零零的命令行界面 这时候应该怎么办呢?首先当然是想进入系 ...
- Datanode 怎么与 Namenode 通信?
在分析DataNode时, 因为DataNode上保存的是数据块, 因此DataNode主要是对数据块进行操作. A. DataNode的主要工作流程 客户端和DataNode的通信: 客户端向Dat ...
- vs _ 用户代码片段 _ html模板
自定义模板:首选项 -> 用户代码片段 - >(如果没有自己创个)html.json t : 表示缩进 n:表示换行 ----------------------------------- ...
- WPF Devexpress 控件库中ChartControl 实现股票分时走势图
概要 从事金融行业开发 ,会接触些图表控件,这里我分享一下自己基于DevExpress.Charts.v16.2开发的股票分时走势图的经验. 附上源码:点击跳转 如果需要讨论,Q群:580749909 ...