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. 中国省市区地址三级联动插件---jQuery Distpicker

    插件描述:distpicker是一款可以实现中国省市区地址三级联动jQuery插件.它使用简单,简单设置即可完成中国省市区地址联动效果. [官网]https://fengyuanchen.github ...

  2. [BZOJ 3796]Mushroom追妹纸

    [BZOJ 3796]Mushroom追妹纸 题目 Mushroom最近看上了一个漂亮妹纸.他选择一种非常经典的手段来表达自己的心意——写情书.考虑到自己的表达能力,Mushroom决定不手写情书.他 ...

  3. maven打包需要设置main-class的插件写法

    maven打包需要设置main-class的插件写法 <build> <plugins> <plugin> <groupId>org.apache.ma ...

  4. AVPlayer的使用,带缓冲

    #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...

  5. codeforces 391E2 (【Codeforces Rockethon 2014】E2)

    题目:http://codeforces.com/problemset/problem/391/E2    题意:有三棵树.每棵树有ni个结点,加入两条边把这三棵树连接起来,合并成一棵树.使得合并的树 ...

  6. (IT/互联网行业)你给自己当前的职位拼几分?(评分标准,个人看法,勿喷~)

    常常有身边的关系好的朋友或网友.问如今我该不该跳槽的问题. 我一般给他们的答复你能给当前的工作拼几分. 下面是我自己总结的一个评分标准.如有不当之处,勿喷~ --------------------- ...

  7. 关于oracle 11g导出数据时 报 ORA 1455错误的处理

    因为导出的该用户的表可能存在空数据表,那么可能就会出现此其异常. 首先:  查看:     SQL>show parameter deferred_segment_creation;  假设为T ...

  8. CF555B Case of Fugitive

    题目大意 有一些不相交线段和一些桥,桥可以架在两个相邻的线段上.求现有的桥是否可以使所有线段连通. 题解 在两个线段上架桥,桥的长度在一个范围内,相当于一个长度的区间,一个桥只有一个长度,相当于一个长 ...

  9. hdu 2846(字典树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  10. hdoj--5233--Gunner II(map+queue&&二分)

     Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...