现在的位置: 首页 > 综合 > 正文

hdu 1729 SG函数

2013年10月14日 ⁄ 综合 ⁄ 共 2013字 ⁄ 字号 评论关闭

Stone Game

Time Limit : 5000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 6

Font: Times New Roman | Verdana
| Georgia

Font Size:

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

这个sg郁闷了我好久,请教了一些人,让我百度一下,还是自己看吧。。。。。。 
首先我们假设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这个点)。。。。(原理1,,只要能够到达必败点则一定是必胜点,,2,,每一步只能到达必胜点则这点一定是必败点 
接下来分三种情况讨论: 
 当 c == t  时则此时一定是必败点直接返回0; 
当c > t 时 此时C是必胜点只要返回C的后继集合中的最小即可。。。而此时最小是s-c 
s  s-1   s-2  s-3 .....  c .. .. t   t-1  t-2  t-3   .................... 1 
0   1  2   3   .....  s-c ...   0 
而当 c < t 时此时将t当做s继续递归下去。。应为刚开是的s始终为必败点。。。 

我的代码 
#include<stdio.h> 
#include<math.h> 
int getsg(int s,int c) 
{ 
  int t; 
  t=(int)sqrt(s); 
 while(t*t+t>=s) 
  { 
               t--; 
    } 
       if(c>t) 
          return s-c; 
     else if(c==t) 
           return 0; 
       else 
            return getsg(t,c); 
} 
int main() 
{ 
      int t,x,y,count=1,c,i; 
  while(scanf("%d",&t),t) 
 { 
             
             
               c=0; 
            for(i=0;i<t;i++) 
         { 
                       scanf("%d%d",&x,&y); 
                    c^=getsg(x,y); 
          } 
               printf("Case %d:\n",count++); 
           if(c) 
                   printf("Yes\n"); 
                else 
                    printf("No\n"); 
 } 
       return 0; 
}

抱歉!评论已关闭.