ZOJ1913 Euclid's Game (第一道简单的博弈题)
题目描述:
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 (第一道简单的博弈题)的更多相关文章
- python几道简单的算法题
最近看了python的语法,但是总感觉不知道怎么使用它,还是先来敲敲一些简单的程序吧. 1.题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十 ...
- 作业 -- 几道简单的Python题
1.编写程序,要求生成10240个随机[0,512)之间的整数,并统计每个元素出现的次数. 2.编写程序,要求当用户输入一个列表和两个整数作为下标时,程序可以使用切片获取并输出列表中截取两个下标之间的 ...
- [POJ1082&POJ2348&POJ1067&POJ2505&POJ1960]简单博弈题总结
鉴于时间紧张...虽然知道博弈是个大课题但是花一个上午时间已经极限了... 希望省选过后再回过头来好好总结一遍吧. 接下来为了看着顺眼一点...还是按照难度顺序吧 POJ1082 一道最简单的博弈 ...
- 留念 C语言第一课简单的计算器制作
留念 C语言第一课简单的计算器制作 学C语言这么久了. /* 留念 C语言第一课简单的计算器制作 */ #include<stdio.h> #include<stdlib.h ...
- 创建第一个简单的AI分类器
from sklearn import tree# 第一个简单的分类器features = [[140, 1], [130, 1], [150, 0], [170, 0]] #列表左边的变量代表水果的 ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- MFC入门(一)-- 第一个简单的windows图形化界面小程序(打开计算器,记事本,查IP)
////////////////////////////////序//////////////////////////////// 大约三年前,学过一些简单的编程语言之后其实一直挺苦恼于所写的程序总是 ...
- 第一道防线__SpringMVC配置拦截器
这几天在公司自己开发一个小系统,但是系统的安全性也得考虑,起初没注意,赶急就光关心业务逻辑和实现效果.最后老大一出手,就把最严重的问题指出来了,他说你这根本没安全性可言,于是我试着将公司使用的spri ...
- hdu 1520 Anniversary party(第一道树形dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
随机推荐
- Sublime 3 如何设置xftp 排除文件夹“bower_components”,“node_modules”
“bower_components”,“node_modules”这个文件夹,作为模块得引用文件,不需要下载本地进行编码,这里得文件非常多,若是不把这个两个文件夹排除掉掉话,通过xftp下载所有文件的 ...
- ubuntu 加载新硬盘或分区
查看目前挂载情况 df -lh 查看新的空间或硬盘 fdisk -lu 新硬盘分区 fdisk /dev/sda 输入m 根据提示输入n(创建一个分区) 然后输入e(扩展分区) 输入分区数1 然后指定 ...
- Mosquitto搭建Android推送服务(四)Mosquitto服务器用户登录与权限配置
文章钢要: 1.对服务器进行多用户配置 2.根据不同用户给予不同权限 一.Mosquitto的用户机制 mosquitto中可以添加多个用户,只有使用用户名和密码登陆服务器才允许用户进行订阅与发布操作 ...
- ios .a和.framework
创建Aggregate来合并模拟器和真机通用的framework 然后在Build Phases下New Run Script Phase创建合并脚本: # Constants SF_TARGET_N ...
- C# winform 跨线程更改窗体控件的属性
当winform程序中新开一个线程,是无法改变主线程中窗体控件的属性的,否则运行时会报错. 若想在其他线程中控制主线程中的窗体控件,则必须利用BeginInvoke方法. 例如:添加一个名为textb ...
- web 开发入门
一 :进入Web JAVASE:标准 - standard JAVA 桌面程序 GUI SOCKET JAVAEE:企业-浏览器控制 WEB 二:软件结构: c/s :clie ...
- 解决Eclipse Failed to write core dump. Minidumps are not enabled by default on client versions
-XX:+CreateMinidumpOnCrash 如果还有问题,就只能看日志了 T_T
- Android Studio上NDK/JNI开发环境问题
基础环境: 操作系统 —— Windows 7 Android Studio —— 1.5.1(android-studio-bundle-141.2456560-windows.exe) NDK — ...
- 常用Javascript语法
1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...
- vim简明教程
在shell中新建一个文件 # vim a.txt vim有三种模式:一般模式.插入模式.底行模式 三种工作模式 1.命令模式 移动光标 hjkl yy 复制 nyy 从光标向下复制n行 0 移动光标 ...