hdu------(1525)Euclid's Game(博弈决策树)
Euclid's Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2074 Accepted Submission(s): 924
Problem 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 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
Source
University of Waterloo Local Contest 2002.09.28
题目的意思:
有两个正整数数,a,b 轮流减去两个数min(a,b)中的倍数,谁最后得到o谁就won,注意两个人都可以对这两个数具有同样的操作......
代码:
开始写一个决策树,不带路径压缩,然后玛德,无限tle...
超时的代码:
1 #include<cstring>
2 #include<cstdio>
3 bool flag=false;
4 int cont=1;
5 void botree(int n,int m){
6 if(n<m)n^=m^=n^=m;
7 if(n%m)
8 for(int i=1;i*m<n;i++){
9 cont++;
10 botree(n-i*m,m);
11 }
12 else {
13 if(cont&1) flag=true;
14 cont=1;
15 }
16 }
17 int main(){
18 int n,m;
19 //freopen("test.in","r",stdin);
20 while(scanf("%d%d",&n,&m),n+m){
21 flag=false;
22 botree(n,m);
23 if(flag) printf("Stan winsn");
24 else printf("Ollie winsn");
25 }
26 return 0;
27 }
然后进行了优化之后.....得到这样的代码:
1 /*Problem : 1525 ( Euclid's Game ) Judge Status : Accepted
2 RunId : 11528629 Language : C++ Author : huifeidmeng
3 Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta*/
4
5 #include<cstring>
6 #include<cstdio>
7 bool flag=false;
8 int cont=1,cc=0;
9 void botree(int n,int m){
10 if(n<m) n^=m^=n^=m;
11 if(m>0&&((n%m)&&n/m==1)){
12 cont++;
13 botree(n%m,m);
14 }
15 else if(cont&1) flag=true;
16 }
17 int main(){
18 int n,m;
19 //freopen("test.in","r",stdin);
20 while(scanf("%d%d",&n,&m),n+m){
21 flag=false;
22 cont=1;
23 botree(n,m);
24 if(flag) printf("Stan winsn");
25 else printf("Ollie winsn");
26 }
27 return 0;
28 }
- C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)
- windows添加静态路由
- C++继承与派生(原理归纳)
- Linux下如何查看自己的服务器有没有无线网卡
- WAMP配置虚拟主机
- linux开关端口问题
- Python 3.6实现单博主微博文本、图片及热评爬取
- 用Django实现一个可运行的区块链应用
- Python的dict实现原理及与Java的比较探究
- 关于位域如何节省内存(C++)
- mysql的小知识点(关于数据库的导入导出 对于windows)
- Python网络编程中的套接字名和DNS解析
- hdu 4009 Transfer water(最小型树图)
- NumPy二元运算的broadcasting机制
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- PHP内置函数生成随机数实例
- PHPStudy下如何为Apache安装SSL证书的方法步骤
- PHP的mysqli_thread_id()函数讲解
- thinkPHP框架中layer.js的封装与使用方法示例
- OpenCV+python实现实时目标检测功能
- 在tensorflow实现直接读取网络的参数(weight and bias)的值
- python tkiner实现 一个小小的图片翻页功能的示例代码
- 在Pytorch中使用Mask R-CNN进行实例分割操作
- PHP类的自动加载机制实现方法分析
- strpos() 函数判断字符串中是否包含某字符串的方法
- Laravel框架基于ajax和layer.js实现无刷新删除功能示例
- 详解php伪造Referer请求反盗链资源
- Laravel框架基于ajax实现二级联动功能示例
- django haystack实现全文检索的示例代码
- 基于Python下载网络图片方法汇总代码实例