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 ...
随机推荐
- Vue 大量data及rules的data选项结构组织
如果Vue文件需要很多的data成员及表单验证,建议使用类似结构 export default{ data(){ const model = { username: 'suzhen', passwor ...
- Android 开发学习进程0.16 layout_weight属性 R文件关联XML Module
layout_weight属性 layout_weight属性我们常常用到,但有时候会发现它还有一些奇怪的属性,比如大多数使用时会把宽度设置成0,但要是宽度不设置成0会有什么效果? layout_we ...
- JavaScript中的正则表达式详解
摘要:javascript中的正则表达式作为相当重要的知识,本文将介绍正则表达式的相关知识和用法. 正则表达式(Regular Expression)是一门简单语言的语法规范,是强大.便捷.高效的文本 ...
- 快速搭建一个Vue-cli项目(简单到爆炸)
引言: 2013-2020年,这7年是web前端技术的一个高速发展期,也是前端开发岗位的考验期. 在我接触前端时,JQuery十分热门,开发者从原生JS到JQ的应用,可以说是大大提高了开发效率,也被广 ...
- JDK1.8源码学习-LinkedList
JDK1.8源码学习-LinkedList 目录 一.LinkedList简介 LinkedList是一个继承于AbstractSequentialList的双向链表,是可以在任意位置进行插入和移除操 ...
- leetcode刷题记录——栈和队列
题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...
- 调试tomcat9.0.19源码
本文所用到的环境: IntelliJ IDEA Apache Maven 3.3.9 jdk1.8 1. 查看Tomcat版本 $TOMCAT_HOME\bin\version.bat D:\Prog ...
- 仿京东BOE官网 css代码
* { margin: 0; padding: 0; border: 0; list-style: none; } .box { width: 1518px; height: 1300px; marg ...
- (新手向)N皇后问题详解(DFS算法)
非常经典的一道题: N皇后问题: 国际象棋中皇后的势力范围覆盖其所在的行.列以及两条对角线,现在考察如下问题:如何在n x n的棋盘上放置n个皇后,使得她们彼此互不攻击 . 免去麻烦我们这里假定n不是 ...
- mybatis批量添加数据的三种方式
原文地址:https://www.cnblogs.com/gxyandwmm/p/9565002.html