题目描述:

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. amazeui折叠面板智能化展开

    2016年12月12日 21:05:18 星期一 场景: 我拿这个组件用作管理后台的侧边栏 效果: 根据当前访问的url不同, 展开不同的面板 amazeui折叠面板 js代码: <script ...

  2. 设计模式--代理模式Proxy(结构型)

    一.代理模式 为其他对象提供一种代理以控制对这个对象的访问. 代理模式分为四种: 远程代理:为了一个对象在不同的地址空间提供局部代表.这样可以隐藏一个对象存在于不同地址空间的事实. 虚拟代理:根据需要 ...

  3. Python使用TuShare将股票数据保存到Oracle数据

    TuShare是个获取股票数据的模块包,我们进行分析,需要将股票数据保存到本地,避免每次都从网上获取,由于本机装有ORCALE,以ORACLE为例介绍如何保存股票数据到本地. 一.大致思路:我们先获取 ...

  4. UILabel 的一个蛋疼问题

    一.问题描述 在iOS8以下版本,numberOfLines设置为0,编译警告Automatic Preferred Max Layout Width before iOS8.0,同时不能换行. 二. ...

  5. 关于settimeout 和for循环

    for(var i=0;i<3;i++){ setTimeOut(function(){ console.log(i) },500) }; 执行结果:3,3,3 ---------------- ...

  6. FusionCharts和highcharts 饼图区别!

    FusionCharts ---------------脚本--------------- <script src="../../../fashioncharts/js/FusionC ...

  7. Java异常处理机构(日常笔记)

    try{ 需要保护的代码块 }catch(异常类型 实例){ 捕捉到异常时的代码处理块 }[可有0~多个catch语句] finaly{ 不管异常是否发生都要执行的代码块}

  8. CSS 清除默认样式

    通常有以下几句就够了: *{margin:0;padding:0} li{list-style:none} img{vertical-align:top;border:none} 如果你想写全也可以: ...

  9. JS string 截取

    subStubstring(a,b); a:开始索引 b:结束索引 subStr(c,d) c:开始索引 d:截取数量.

  10. 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】

    一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...