hdoj 1729 Stone Games(SG函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729
看了题目感觉像Nim,但是有范围限制,有点不知道SG函数该怎么写
看了题解,最后才明白该怎么去理解 。
首先进行对s和c进行分类,
1、c = 0 的时候,无论怎样都填不满,直接跳过;
2、c = s 的时候,先手必败,即是P态;
3、c < s 的时候,可以分为两种情况:
1)c^2 + c < s 的时候,递归
2)c^2 + c > s 的时候,先手必胜,即N态
int mex(int s, int c){
|| s == c)
;
int q = sqrt(s);
while( q+q*q >= s)
q--;//最大的不能一次填满的数
if(c > q) return s-c;
else return mex(q,c);
}
主要的理解难点就是 mex(s,c)和mex(k,c)为什么是等价的,个人是这么理解的,
当跑到第一个q+q*q<s的时候,如果c>q,那么可以一次填满,
但如果c<q,不可能一次填满,此时可以分割成两个事件,mex(c,k)和mex(s,k),
其中对于mex(s,k),mex(s,s) 是先手必败,即P态,mex(s,k+1)...mex(s,s-1)都是先手必胜,即N态,
所有移动都导致N态局面的是P态,所以显然mex(s,k)是P态,那么mex(s,k)显然与mex(k,c)等价
#include<stdio.h>
#include<cmath>
#include<cstring>
using namespace std;
;
int mex(int s, int c){
|| s == c)
;
int q = sqrt(s);
while( q+q*q >= s)
q--;//最大的不能一次填满的数
if(c > q) return s-c;
else return mex(q,c);
}
int main(){
int N;
int ans;
int s, c;
;
while(~scanf("%d",&N)&&N){
ans = ;
; i < N; ++i){
scanf("%d%d",&s,&c);
ans = ans^mex(s,c);
}
printf("Case %d:\n",++T);
if(ans)
puts("Yes");
else
puts("No");
}
}
hdoj 1729 Stone Games(SG函数)的更多相关文章
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- UVA 11927 - Games Are Important(sg函数)
UVA 11927 - Games Are Important option=com_onlinejudge&Itemid=8&page=show_problem&catego ...
- LightOJ 1296 Again Stone Game(sg函数)题解
题意:每次必须拿且只能拿不超过一半的石头,不能拿为败 思路:显然算出每个的sg函数,但是范围1e9显然不能直接打表.所以先打表找规律,发现偶数一直是自己的一半,奇数好像没规律.偶数x的sg函数值是x/ ...
- HDOJ 1848(SG函数)
对于SG函数来说,sg[y]=x的意义为,x与y的输赢状态是相同的 sg[y]=mex(y)的定义与n.p点的定义是相同的 #include<iostream>#include<cs ...
- hdu 1729 Stone Game
Stone Game HDU - 1729 题意: 给定n个箱子,每个箱子的容量为si,每个箱子里最初有ci个石子,每次放入石子不能超过放入前的石子数的平方,谁无法继续放入石子就算输. /* 这是 ...
- 新年第一发--HDU1848--Fibonacci again and again(SG函数)
Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;F(n)=F(n-1)+F(n-2 ...
- 【转】博弈—SG函数
转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...
- hdu1536&&hdu3023 SG函数模板及其运用
S-Nim Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status ...
- HDU1848 Fibonacci again and again SG函数
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
随机推荐
- ecshop显示所有分类树栏目
1.找到 category.php 和goods.php 两个文件修改: $smarty->assign('categories', get_categories_tree(0)); // 分类 ...
- 【英语】Bingo口语笔记(55) - work系列
- 【英语】Bingo口语笔记(78) - let系列
- 添加navbar以及上面的左右按钮代码
UINavigationBar *navBar = [[UINavigationBaralloc] initWithFrame:CGRectMake(0, 0, 824, 44)]; navBar.b ...
- JVM——新生代与老年代
首先看在JVM的堆中,按代的划分: Young:主要是用来存放新生的对象. Old:主要存放应用程序中生命周期长的内存对象. Permanent:是指内存的永久保存区域,主要存放Class和Meta的 ...
- 关于用netbeans和xdebug调试php的配置
之前用过一段时间在apache,netbeans下通过xdebug调试.感觉不错,最近事情不多想从新配置下,是基于最新版本的php5.4做的,后来参考了下xdebug的官网说明完成的.官网地址:htt ...
- [搜片神器]直接从DHT网络下载BT种子的方法
DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr DHT系 ...
- Drupal如何处理系统变量?
Drupal的系统变量都保存在数据库variable表中: 然后,开发人员可以通过下面的API函数操作这些系统变量: function variable_get($name, $default = N ...
- stitch image app (Qt)
result how to use? source code http://7qnct6.com1.z0.glb.clouddn.com/dropSelect.rar
- jquery自动将form表单封装成json的具体实现
前端页面:<span style="font-size:14px;"> <form action="" method="post&q ...