题目:http://acm.hdu.edu.cn/showproblem.php?pid=1729

思路:理解错题目了,以为SG模板直接套就行了。后来队友说了那个ci是不断变化的。那么每次可以放的石头数量也是不断变化的。但是按照自己的思路改了改模板,(TLE),最后看了题解理解了一下。

看了网上好多题解,有一点想法了:题解都是找到一个q,使得

q+q*q < si && (q+1) + (q+1)*(q+1) >= si --------------------------------------------------(****)

如果 ci > q, 先收获胜 返回si - ci。如果ci < q, 则递归SG(q, ci),

为什么找这样的q呢?为什么这样判断呢?

倒着推理,假如我玩这个游戏,我先找到q,使得(****)成立,然后判断ci和q的关系

如果q < ci, 也就是 ci >= (q+1), 那么我就能放满,先手获胜 返回si-ci 。因为GS(si,si) = 0,(空间size大小为S,begin为S,谁都不能放,先手败)必败状态,GS(si, si-1)的后继只有GS(si, si) ,所以GS(si, si-1) = 1, 同理 GS(si, si-2) = 2 , GS(si, ci) = si - ci;

如果q = ci, 肯定是必败的。我这次放不满,但是下次的肯定能放满(注意(***)

如果ci < q, 不能确定状态,所以要递归SG(q, ci)。

多想想应该就理解了

AC代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define in freopen("in.txt", "r", stdin);
typedef long long ll;
const int inf = 0x3f3f3f3f; int SG(int si, int ci) {
int q = sqrt((double) si);
while(q+q*q >= si) q--;//找到刚好使 q+q*q < si && (q+1)+(q+1)*(q+1) >= si
if(ci > q) // 先手获胜
return si - ci;
else //无法判断,递归
return SG(q, ci);
} int main() {
//in;
int n, si, ci;
int j = 1;
while(~scanf("%d", &n) && n) {
int sum = 0;
while(n--) {
scanf("%d %d", &si, &ci);
sum ^= SG(si, ci);
}
printf("Case %d:\n", j++);
if(sum)//原来这里写反了 很郁闷
printf("Yes\n");
else
printf("No\n");
}
}

HDU1729 Stone Game的更多相关文章

  1. POJ1740A New Stone Game[组合游戏]

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5769   Accepted: 3158 ...

  2. timus 1180. Stone Game 解题报告

    1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...

  3. HDU 4048 Zhuge Liang's Stone Sentinel Maze

    Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/327 ...

  4. POJ 1740 A New Stone Game

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5453   Accepted: 2989 ...

  5. Light OJ 1296 - Again Stone Game (博弈sg函数递推)

    F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  6. poj 1115 Lifting the Stone 计算多边形的中心

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. 【POJ】A New Stone Game(博弈论)

    http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...

  8. HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)

    A simple stone game                                                                                  ...

  9. POJ 1740 A New Stone Game(普通博弈)

    A New Stone Game 题意: 对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆.最后谁无子可取即输 ...

随机推荐

  1. UVA-11020(BST)

    题意: 给n个点,一个点(x,y)有优势时满足不存在点(fx,fy)使得fx<x,fy<=y或fx<=x,fy<y;问当前有多少个有优势点; 思路: 学习BST的入门题,代码是 ...

  2. codeforces 598E E. Chocolate Bar(区间dp)

    题目链接: E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. loj514模拟只会猜题意

    果然是道模拟... 一开始想线段树 看了一眼数据范围:“这tm不是前缀和吗” 然后水过 #include<iostream> #include<cstdio> #include ...

  4. [JSOI 2015] 最大公约数

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4488 [算法] 不妨首先枚举左端点 注意到对于任意一个正整数n , 其质因子个数是l ...

  5. 关于jquery登录的一些简单验证。

    获取值之后的判断 $(function () { $("#btlogin").click(function () { var txtaccount = $("#txtac ...

  6. mysql auto reset

    参数说明: •相关参数说明: •dataSource: 要连接的 datasource (通常我们不会定义在 server.xml) defaultAutoCommit: 对于事务是否 autoCom ...

  7. Java探索之旅(3)——选择与循环

    1.选择结构与输出 ❶Switch语句: Switch表达式必须算出 char,byte,short,int类型数值之一,总是括号括住:Value1----ValueN,对应有相同数据类型且为常量或者 ...

  8. chef语法和案例

    1. 添加用户 $ vim create_user.rb -----------------------------------> user 'charlie' do //创建一个uid为88的 ...

  9. CSS, Sass, SCSS 关系

    Sass(Syntactically Awesome Style Sheets) ,是一种css预处理器和一种语言, 它可以用来定义一套新的语法规则和函数,以加强和提升CSS. 它有很多很好的特性,但 ...

  10. Error creating form bean of class com.onlinebookstore.presentation.CatalogBean

    Error creating form bean of class com.onlinebookstore.presentation.CatalogBean 可能是action form未编译 这个问 ...