题目: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. Leetcode 1002. Find Common Characters

    python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...

  2. STL stl_uninitialized.h

    stl_uninitialized.h // Filename: stl_uninitialized.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com ...

  3. git branch detached from jb4.2.2_1.0.0-ga

    /*************************************************************************** * git branch detached f ...

  4. Trie(前缀树/字典树)及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  5. GCC提供的几个內建函数

    参考 https://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Other-Builtins.html#Other-Builtins https://en.wikipe ...

  6. 使用swift命令遭遇503错误

    使用swift命令遭遇503 Internal Server Error.这个问题可能有很多种原因,这里只描述下我当前遇到的一种情况. 我们实验环境下的swift设有3个zone,有1个proxy n ...

  7. RESTEasy常用注解

    一.@Path,标注资源类或方法的相对路径          Path参数的形式有三种:          1.固定值          2.纯正则表达式          3.固定值和正则表达式的混 ...

  8. 一个有关Golang变量作用域的坑

    转自:http://tonybai.com/2015/01/13/a-hole-about-variable-scope-in-golang/ 临近下班前编写和调试一段Golang代码,但运行结果始终 ...

  9. Spring开发包介绍

    -----------------siwuxie095                         核心开发包         建立 Spring 工程时,需要引入 Spring 的开发包,否则无 ...

  10. pig入门教程(2)

    本文可以让刚接触pig的人对一些基础概念有个初步的了解. 本文的大量实例都是作者Darran Zhang(website: codelast.com)在工作.学习中总结的经验或解决的问题,并且添加了较 ...