Stone Game

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3051    Accepted Submission(s):
939

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.

 
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.
 
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
3
2 0
3 3
6 2
2
6 3
6 3
0
 
Sample Output
Case 1:
Yes
Case 2:
No
 
题意:n个盒子,每个容量为si,已装入ci个石子,两个玩家,分别往盒子中放入石子,每次放入的石子数量不能超过盒子中石子数量的平方,没有盒子可以放的玩家败。
最开始想用之前的模版来做,求sg值,但是这道题的“点数”为1000000,用之前的模版显然超时,所以果断看了题解,大牛们用的是dfs求每个点的sg值。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define N 1000005 int Sg(int c,int s)
{
int t=sqrt(s);
while(t*t+t>=s)
t--;
if(c>t)
return s-c;
else
return Sg(c,t);
} int main()
{
int n,cnt=;
while(scanf("%d",&n)!=EOF&&n)
{
int s,c,res=;
while(n--)
{
scanf("%d%d",&s,&c);
res^=Sg(c,s);
}
printf("Case %d:\n",++cnt);
if(res)
printf("Yes\n");
else
printf("No\n");
}
return ;
}

HDU_1729_sg函数(dfs)的更多相关文章

  1. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)B 杨老师的游戏【暴力/next-permutation函数/dfs】

    链接:https://www.nowcoder.com/acm/contest/116/B 来源:牛客网 题目描述 杨老师给同学们玩个游戏,要求使用乘法和减法来表示一个数,他给大家9张卡片,然后报出一 ...

  2. 黑白图像(DFS)

    输入一个n*n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数.如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块.如图6-11所示的图形有3个八连块. 图6-11  拥有3 ...

  3. POJ 1321 棋盘问题(DFS板子题,简单搜索练习)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44012   Accepted: 21375 Descriptio ...

  4. 广度优先(bfs)和深度优先搜索(dfs)的应用实例

    广度优先搜索应用举例:计算网络跳数 图结构在解决许多网络相关的问题时直到了重要的作用. 比如,用来确定在互联网中从一个结点到另一个结点(一个网络到其他网络的网关)的最佳路径.一种建模方法是采用无向图, ...

  5. POJ 1321 - 棋盘问题 - [经典DFS]

    题目链接:http://poj.org/problem?id=1321 Time Limit: 1000MS Memory Limit: 10000K Description 在一个给定形状的棋盘(形 ...

  6. 记忆化搜索(DFS)--How many ways

    How many ways 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下:1.机器人一开始在棋盘的起始点并有起始点所标有的能量.2. ...

  7. bfs与dfs基础

    bfs像二叉树的层序遍历 像这个图走bfs就{1, 2, 3, 4, 5, 6, 7, 8}这样走: dfs就{1, 2, 5, 6, 3, 7, 8, 4}. bfs与queue相结合,走到哪就把哪 ...

  8. 递归一题总结(OJ P1117倒牛奶)

    题目:                    农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另 ...

  9. ZOJ 2412 Farm Irrigation

    Farm Irrigation Time Limit: 2 Seconds      Memory Limit: 65536 KB Benny has a spacious farm land to ...

随机推荐

  1. 【Codeforces 522A】Reposts

    [链接] 我是链接,点我呀:) [题意] 有人转载官方号的动态. 又有其他人转载其他人转载的动态. 问你最长的一条转载动态的链有多长. [题解] 用map把每个人的英文都转成小写的 然后从map中获取 ...

  2. Object Detection: To Be Higher Accuracy and Faster

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51597496 在深度学习中有一类研究热 ...

  3. Mysql中文乱码以及导出为sql语句和Excel问题解决

    Mysql中文乱码以及导出为sql语句和Excel问题解决 这几天基于Heritrix写了一个爬虫,用到mysql,在导入导出数据时,遇到一些乱码问题,好不容易解决了,记录一下,以备查看.一.导出数据 ...

  4. 清北学堂模拟赛d7t6 拯救世界

    分析:如果题目中没有环的话就是一道裸的最长路的题目,一旦有环每个城市就会被救多次火了.把有向有环图变成有向无环图只需要tarjan一边就可以了. #include <bits/stdc++.h& ...

  5. python 多个相同字符串

    1.相同字符串str,重复打印n次.   str=str*n 2.填充字符串str为指定宽度n,左边填充0. str.zfill(n)

  6. python中添加日志记录到文件

    1.实现python日志功能 2.只输出到文件,不输出到控制台 #encoding:utf-8 import logging from common import path_util logging_ ...

  7. 使用JQUERY的flexselect插件来实现将SELECT下拉菜单变成自动补全输入框

    这也是下拉列表太长了之后,使用的同事提出来的意见, 然后,本来开始想将DJANGO的那个后台下拉菜单移植过来的,但发现不现实,也麻烦, 就找了几个JQUERY的插件测试了一下,最后选中了flexsel ...

  8. nyoj_37_回文字符串_201403121649

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当 ...

  9. ACM数论总结

    ACM数论总结 http://blog.csdn.net/xieshimao/article/details/6425099 断断续续的学习数论已经有一段时间了,学得也很杂,现在进行一些简单的回顾和总 ...

  10. [bzoj2743][HEOI2012]采花_树状数组

    采花 bzoj-2743 HEOI-2012 题目大意:给定n朵花,每朵花有一个种类,m次询问:一段区间中至少出现两朵花的种类的个数. 注释:$1\le n,m\le10^6$. 想法:这个题超级像H ...