Stone game
Time Limit: 5000/1000 MS (Java/others) memory limit: 65535/32768 K (Java/Others)
Total submission (s): 2539 accepted submission (s): 741
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 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.
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
32 03 36 226 36 30
Sample output
Case 1: yescase 2: No/* I thought it was very difficult. As a result, I read my blog or the SG function time: 2014-8-27 21-18 */# include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; int get_sg (INT sum, int X) {int d = SQRT (SUM) + 1; while (D * D + D> = sum) d --; // find the critical point of failure if (x> D) // D * D + D = sum D-1 is a winning state. If it is greater than or equal to D, it will be defeated, and the minimum value is sum-d Return sum-X; // return the minimum value that can be obtained, that is, the SG value. // minus X and not D, that is, the minimum number. The error message "wa" is written many times. elsereturn get_sg (D, X ); // start from the critical value and then look down, find the return} int main () {int N; int D = 0; while (scanf ("% d", & N), n) {int ans = 0; int C, sum; For (INT I = 0; I <n; I ++) {scanf ("% d", & sum, & C); ans ^ = get_sg (sum, c);} printf ("case % d: \ n", ++ D); If (ANS) printf ("Yes \ n"); elseprintf ("NO \ n");} return 0 ;}
Hangdian 1279 stone game (classic game)