题目描述:

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. php二进制安全的含义

    PHP里,有string的概念.string里,每个字符的大小为byte(与PHP相比,Java的每个字符为Character,是UTF8字符,C语言的每个字符可以在编译时选择). byte里,有AS ...

  2. C语言小结

    1.输入输出: char s; printf("Enter a string"); scanf("%s",&s); printf(''Hello,%s& ...

  3. 聊一聊log4j2配置文件log4j2.xml

    一.背景 最近由于项目的需要,我们把log4j 1.x的版本全部迁移成log4j 2.x 的版本,那随之而来的slf4j整合log4j的配置(使用Slf4j集成Log4j2构建项目日志系统的完美解决方 ...

  4. hdu 2222 Keywords Search

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...

  5. 第五课 CSS3 and H5 知识点

    概要:CSS3美化样式.自定义字体图标.滤镜设置.CSS3选择器.transform2D转换.新增表单控件.vaild表单验证.表单样式美化等. 属性选择器: E[attr]只使用属性名,但没有确定任 ...

  6. 在nginx中配置ip直接访问的默认站点

    一台机子部署多个网站,我们直接访问ip (外网内网都一样)提示无法访问或404. 因为有多个网站,我们想指定某个网站为IP访问默认的网站,如何操作呢? 解:在Listen ip:port; 这个指令行 ...

  7. ExtJs基础知识总结:自定义弹窗和ComboBox自动联想加载(四)

    概述 Extjs弹窗可以分为消息弹窗.对话框,这些弹窗的方式ExtJs自带的Ext.Msg.alert就已经可以满足简单消息提示,但是相对复杂的提示,比如如何将Ext.grid.Panel的控件显示嵌 ...

  8. 使用 ApacheBench 进行轻量级压力测试

    ApacheBench 是 Apache Http Server 附带的一个轻量级压力测试功能 先下载一个Apache Http Server :http://httpd.apache.org/ 解压 ...

  9. 前端js调用七牛制作评价页面案例

    一.需求 公司所有的上传页面都用七牛,前端不免要直接调用七牛的代码进行上传,以下是一个实现七牛上传的案例,制作一个常见的商品评价页面,页面需求很常见当上传到第五章图片的时候,上传按钮消失,上传需要显示 ...

  10. Xcode 突然有部分文件不显示

    今天突然Xcode抽风了,突然我的项目中的文件好多不显示出来,本地文件夹都有,见鬼了..... 真心坑死了,于是乎就找度娘了,果然被我发现了 看见这个蓝色小时钟没呀,就是他在捣鬼,我点击一下变成灰色就 ...