题目描述:

Euclid's Game


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

分析:

若存在x=k*y+r(k>1)则谁先面对x=k*y+r(k>1),即x>2*y的局势谁赢,
因为此时可以拿成 (x%y,y) 或 (x%y+y,y)这两种局势,而这两种局势中要达到最终(num,0)步数必然
一奇一偶(相差一步),则此时只要选择自己取胜的最优策略就可以了
当然若一开始x<2*y,则看到达(num,0)的步数的奇偶性来决定胜负

而过程类似于欧几里得算法(估计也是这道题为何叫欧几里得的游戏的原因所在),递归就行了。

伪代码:

f(a,b)

if a%b == 0 --->先手赢

else  if  a<2*b ---> 递归

else  if  a>2*b --->当前者赢

end if

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
int ice(int a,int b){
if(a < b)
swap(a,b);
if(a % b == )
return ;
else if(a < *b)//a=b+r,k==1;记步数
return !(ice(a-b,b));//先手赢返回1,否则返回0;
return ;//a>2*y,直接先手赢。 } int main(){
int a,b,c;
while(scanf("%d%d",&a,&b) != EOF){
if(a == && b == )
break;
c = ice(a,b);
if(c)
printf("Stan wins\n");
else
printf("Ollie wins\n"); }
return ;
}

ZOJ1913 Euclid's Game (第一道简单的博弈题)的更多相关文章

  1. python几道简单的算法题

    最近看了python的语法,但是总感觉不知道怎么使用它,还是先来敲敲一些简单的程序吧. 1.题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十 ...

  2. 作业 -- 几道简单的Python题

    1.编写程序,要求生成10240个随机[0,512)之间的整数,并统计每个元素出现的次数. 2.编写程序,要求当用户输入一个列表和两个整数作为下标时,程序可以使用切片获取并输出列表中截取两个下标之间的 ...

  3. [POJ1082&POJ2348&POJ1067&POJ2505&POJ1960]简单博弈题总结

    鉴于时间紧张...虽然知道博弈是个大课题但是花一个上午时间已经极限了... 希望省选过后再回过头来好好总结一遍吧. 接下来为了看着顺眼一点...还是按照难度顺序吧   POJ1082 一道最简单的博弈 ...

  4. 留念 C语言第一课简单的计算器制作

    留念 C语言第一课简单的计算器制作 学C语言这么久了.  /* 留念 C语言第一课简单的计算器制作 */   #include<stdio.h>  #include<stdlib.h ...

  5. 创建第一个简单的AI分类器

    from sklearn import tree# 第一个简单的分类器features = [[140, 1], [130, 1], [150, 0], [170, 0]] #列表左边的变量代表水果的 ...

  6. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  7. MFC入门(一)-- 第一个简单的windows图形化界面小程序(打开计算器,记事本,查IP)

    ////////////////////////////////序//////////////////////////////// 大约三年前,学过一些简单的编程语言之后其实一直挺苦恼于所写的程序总是 ...

  8. 第一道防线__SpringMVC配置拦截器

    这几天在公司自己开发一个小系统,但是系统的安全性也得考虑,起初没注意,赶急就光关心业务逻辑和实现效果.最后老大一出手,就把最严重的问题指出来了,他说你这根本没安全性可言,于是我试着将公司使用的spri ...

  9. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

随机推荐

  1. 关于imageOrientation

    用相机拍出来的照片都含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息.如果我们忽略orientation信心,而直接对照片进行想速处 ...

  2. html5 canvas 实现倒计时 功能

    function showTime(a) { var b = { id: "showtime", //canvasid x: 60, //中心点坐标 X轴; y: 60, //中心 ...

  3. windows系统版本号

    windows操作系统版本号 操作系统 版本号 Windows8.1 6.3 Windows8 6.2 Windows7 6.1 Windows Server 2008 R2 6.1 Windows ...

  4. web.config数据库连接字符串

    我们在做VB数据库经典实例这本书上的五个小例子和做学生信息管理系统时,都需要通过Vb链接数据库,在众多的链接方式中connectionstring字符串应该是较为简单的一种方式,下面我来详细介绍它的一 ...

  5. extarea输入的换行在网页上正常显示

    如图

  6. webpack常用配置总结

    1. webpack简介 webpack 是一个模块打包工具.它使得模块相互依赖并且可构建等价于这些模块的静态资源.相比于已经存在的模块打包器(module bundler),webpack的开发动机 ...

  7. oh my zsh

    简单使用oh my zsh 安装oh my Zsh 安装zsh 安装curl或者wget 下载并安装oh my zsh: curl 下载方式curl -L https://raw.github.com ...

  8. iOS - 如何切图适配各种机型

    关于iPhone6/6+适配问题一直有争议,今天小编专门为大家整理了相关的有效方案,希望对大伙儿有帮助! 移动app开发中多种设备尺寸适配问题,过去只属于Android阵营的头疼事儿,只是很多设计师选 ...

  9. angular1.x的简单介绍 (一)

    angular1.x作为经典的mvc框架,可以创建能够复用的组件,也可进行双向数据绑定.国内的vue.js/avaloon.js都是同类型的框架.适合使用angularjs的项目有大型信息化管理系统: ...

  10. SOUI与WTL

    如果你想使用SOUI最好有点WTL基础,一点点就行了. SOUI不依赖于WTL,但是SOUI的编码风格基本和WTL一样的:SOUI抄袭了WTL的消息处理形式,SOUI的事件处理也是模仿了WTL的消息映 ...