HDU 1729 SG Function

Source: Internet
Author: User
Stone gametime limit: 5000/1000 ms (Java/other) Memory limit: 65535/32768 K (Java/other)Total submission (s): 12 Accepted submission (s): 6Font: Times New Roman | Verdana
| GeorgiaFont Size: Bytes →Problem descriptionthis 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
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. inputthe 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 shoshould not be processed. outputfor 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
 
This SG has been depressing for me for a long time. I have consulted some people and asked Baidu to check it out ......
First, let's assume that C is a must-have point, then C + C * C <S (even if the largest C * C cannot be filled up)
C + 1 is a winning point. C + 1 + (C + 1) * (C + 1)> = s based on the two formulas, we can find the maximum defeat point t smaller than S.
T + 1 ---- S-1 is a winning point, because (t + 1 --- S-1 can all reach the point S ).... (Principle 1, as long as it can reach the point of defeat, it must be a point of victory, 2, each step can only reach the point of victory, then this must be a point of defeat)
The following three cases are discussed:
 When c = TAt this time, it must be a point of failure and 0 is returned directly; 
When C> T, C is the winning point, as long as the minimum value in the subsequent set of C is returned... The minimum value is 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 
When C <t, t is used as s to continue recursion .. It should always be a point of defeat for the newly opened S...
 
My code:
#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; 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.