hdoj 1729 Stone Game(sg函数)

来源:互联网 发布:自助建站系统源码 编辑:程序博客网 时间:2024/05/17 04:47

题目地址:点击打开链接


题意:

现在有n个箱子,每个箱子有一个容积si和一个当前箱子内的石子数ci,两个人轮流往这n个箱子里面放石子,每次放的个数为1-ci*ci。例如箱子里有3颗石子,那么下一个人就可以放1~9颗石子最后不能放入石子的人输。


一开始直接递推sg函数....数据量比较大 必然TLE...


思路:

我们知道s肯定是必败态,此时已经没有可以放入的石子了。


首先我们假设c 是必败点,则有c+c*c<s(即使放了最大的c*c也不能放满) 

而c+1则是必胜点则有c+1+(c+1)*(c+1)>=s根据这两个公式我们可以求出小于s的最大必

败点t。 而t+1----s-1是必胜点因为(t+1---s-1都可以到达s这个点)


接下来分三种情况讨论:


当 c == t  时则此时一定是必败点直接返回0; 


当c > t 时 此时C是必胜点只要返回C的后继集合中的最小即可。。。而此时最小是s-c 
石子数:    s  s-1   s-2  s-3 .....  c .. .. t  
对应的sg值:0    1     2    3 .....  s-c ... 0 


而当 c < t 时此时将t当做s继续递归下去。。因为刚开是的s始终为必败点


参考博客:点击打开链接



代码:

#include<iostream>#include<cmath>#include<cstdio>#include<cstring>using namespace std;int solve(int s, int c){    int p = sqrt(s);    while(p+p*p >= s) p--;    if(c > p) return s-c;    else return solve(p, c);}int main(void){    int n, ca = 1;    while(cin >> n, n)    {        int ans = 0;        for(int i = 0; i < n; i++)        {            int s, c;            scanf("%d%d", &s, &c);            ans ^= solve(s, c);        }        printf("Case %d:\n", ca++);        puts(ans ? "Yes" : "No");    }    return 0;}


Stone Game

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


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
32 03 36 226 36 30
 

Sample Output
Case 1:YesCase 2:No
 

Source
“网新恩普杯”杭州电子科技大学程序设计邀请赛
 

Recommend
lcy
 




0 0
原创粉丝点击