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 785D】Anton and School - 2

    [题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...

  2. web开发如何使用高德地图API(三)点击热点打开信息窗体

    说两句: 以下内容除了我自己写的部分,其他部分在高德开放平台都有(可点击外链访问). 我所整理的内容以实际项目为基础希望更有针对性的,更精简. 点击直奔主题. 准备工作: 首先,注册开发者账号,成为高 ...

  3. Ajax 请求之_请求类型详解

    $.ajax({ url: "规定发送请求的 URL.默认是当前页面.", type: "post", // 请求类型,默认get // 在回调函数中,无需将j ...

  4. fzu 2122

    #include<stdio.h> #include<string.h> #define N 51000 char s1[200],s2[200],s[N]; int main ...

  5. 洛谷 P2997 [USACO10NOV]旗帜Banner

    P2997 [USACO10NOV]旗帜Banner 题目背景 题目大意(by:曹彦臣): 平面上有(0,0)到(n,m)的(n+1)*(m+1)个点.问有多少点对所连的线段不过其他点,且长度在[l, ...

  6. UI层自动化测试框架(一)-简介和环境搭建

    http://blog.csdn.net/ToBeTheEnder/article/details/52302777

  7. UVa 10693 - Traffic Volume

    題目:車速為v,車之間的距離最少為v^2/(2f)(防止裝車),車長為L,問1小時最多能走過幾輛車. 分析:數學.物理.             根據題意能够列出方程:nL + (n-1)d = nL ...

  8. media type

    https://www.sitepoint.com/mime-types-complete-list/ application/base64 https://github.com/dotnet/doc ...

  9. 将tflearn的模型保存为pb,给TensorFlow使用

    参考:https://github.com/tflearn/tflearn/issues/964 解决方法: """ Tensorflow graph freezer C ...

  10. Too-Java:Intellij Idea

    ylbtech-Too-Java:Intellij Idea IDEA 全称 IntelliJ IDEA,是java编程语言开发的集成环境.IntelliJ在业界被公认为最好的java开发工具之一,尤 ...