hdoj 1729 Stone Game(sg函数)
来源:互联网 发布:自助建站系统源码 编辑:程序博客网 时间:2024/05/17 04:47
题目地址:点击打开链接
题意:
现在有n个箱子,每个箱子有一个容积si和一个当前箱子内的石子数ci,两个人轮流往这n个箱子里面放石子,每次放的个数为1-ci*ci。例如箱子里有3颗石子,那么下一个人就可以放1~9颗石子最后不能放入石子的人输。
一开始直接递推sg函数....数据量比较大 必然TLE...
思路:
我们知道s肯定是必败态,此时已经没有可以放入的石子了。
而c+1则是必胜点则有c+1+(c+1)*(c+1)>=s根据这两个公式我们可以求出小于s的最大必
败点t。 而t+1----s-1是必胜点因为(t+1---s-1都可以到达s这个点)
接下来分三种情况讨论:
当 c == t 时则此时一定是必败点直接返回0;
石子数: s s-1 s-2 s-3 ..... c .. .. t
对应的sg值:0 1 2 3 ..... s-c ... 0
而当 c < t 时此时将t当做s继续递归下去。。因为刚开是的s始终为必败点
参考博客:点击打开链接
代码:
#include<iostream>#include<cmath>#include<cstdio>#include<cstring>using namespace std;int solve(int s, int c){ int p = sqrt(s); while(p+p*p >= s) p--; if(c > p) return s-c; else return solve(p, c);}int main(void){ int n, ca = 1; while(cin >> n, n) { int ans = 0; for(int i = 0; i < n; i++) { int s, c; scanf("%d%d", &s, &c); ans ^= solve(s, c); } printf("Case %d:\n", ca++); puts(ans ? "Yes" : "No"); } return 0;}
Stone Game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3282 Accepted Submission(s): 1046
Problem Description
This game is a two-player game and is played as follows:
1. There are n boxes; each box has its size. The box can hold up to s stones if the size is s.
2. At the beginning of the game, there are some stones in these boxes.
3. The players take turns choosing a box and put a number of stones into the box. The number mustn’t be great than the square of the number of stones before the player adds the stones. For example, the player can add 1 to 9 stones if there are 3 stones in the box. Of course, the total number of stones mustn’t be great than the size of the box.
4.Who can’t add stones any more will loss the game.
Give an Initial state of the game. You are supposed to find whether the first player will win the game if both of the players make the best strategy.
1. There are n boxes; each box has its size. The box can hold up to s stones if the size is s.
2. At the beginning of the game, there are some stones in these boxes.
3. The players take turns choosing a box and put a number of stones into the box. The number mustn’t be great than the square of the number of stones before the player adds the stones. For example, the player can add 1 to 9 stones if there are 3 stones in the box. Of course, the total number of stones mustn’t be great than the size of the box.
4.Who can’t add stones any more will loss the game.
Give an Initial state of the game. You are supposed to find whether the first player will win the game if both of the players make the best strategy.
Input
The input file contains several test cases.
Each test case begins with an integer N, 0 < N ≤ 50, the number of the boxes.
In the next N line there are two integer si, ci (0 ≤ ci ≤ si ≤ 1,000,000) on each line, as the size of the box is si and there are ci stones in the box.
N = 0 indicates the end of input and should not be processed.
Each test case begins with an integer N, 0 < N ≤ 50, the number of the boxes.
In the next N line there are two integer si, ci (0 ≤ ci ≤ si ≤ 1,000,000) on each line, as the size of the box is si and there are ci stones in the box.
N = 0 indicates the end of input and should not be processed.
Output
For each test case, output the number of the case on the first line, then output “Yes” (without quotes) on the next line if the first player can win the game, otherwise output “No”.
Sample Input
32 03 36 226 36 30
Sample Output
Case 1:YesCase 2:No
Source
“网新恩普杯”杭州电子科技大学程序设计邀请赛
Recommend
lcy
0 0
- hdoj 1729 Stone Game(sg函数)
- hdu 1729 Stone Game(SG函数)
- HDU 1729 Stone Game (sg函数)
- HDU 1729 Stone Game(SG函数)
- HDU 1729 Stone Game(SG函数变形)
- HDU 1729 Stone Game(思维 & SG函数)
- HDU 1729 Stone Game(博弈 + SG函数)
- HDU 1729 Stone Game (SG函数)
- HDU 1729 Stone Game 【博弈论,SG函数】
- sg函数 hdu 1729 Stone Game
- hdu Stone Game SG函数
- HDU1729 Stone Game (SG函数)
- hdu1729 Stone Game-sg函数
- HUD 1729 Stone Game(SG)
- HDOJ 1729 Stone Game
- Again Stone Game (通过SG函数找规律)
- uva1378 - A Funny Stone Game SG函数
- 【lightOJ】 Again Stone Game (博弈,SG函数)
- 动态添加、插入、删除菜单
- Spring的IOC原理
- springMVC笔记系列(4)——RequestMapping映射特定params和headers
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- iOS ViewController跳转界面的几种方法简单总结
- hdoj 1729 Stone Game(sg函数)
- MFC的消息映射机制
- .gitignore
- thinkphp模板页面多条件if判断的写法
- CKEditor4.2图片上传以及预览
- JAVA方法中的参数用final来修饰的原因
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- 浏览器和js交互、NPAPI之NPRuntime开发
- nodeJs 是什么?你需要先想清楚这个问题,才能学习nodejs (介绍)