Description
Urej loves to play various types of dull games. He usually asks other people to play with him. He says that playing those the can show his extraordinary wit. Recently Urej takes a great interest in a new game, and Erif Nezorf becomes the victim. To get away from suffering playing such a dull game, Erif Nezorf requests your help. The game uses a rectangular paper that consists of w*h grids. The players cut the paper into and the pieces of rectangular sections in turn. In each turn the player can cut either horizontally or vertically, keeping every grids unbroken. After N turns the paper is broken into n+1 pieces, and in the later turn the players can choose any piece to cut. If one player cuts out a piece of paper with a single grid, he wins the game. If These people is both quite clear, you should write a problem to tell whether the one who cut first can win or not.
Input
The input contains multiple test cases. Each test case contains only integers W and h (2 <= W, H <=.) in one line, which is the width and height of The original paper.
Output
For each test case, only one line should is printed. If the one who cut first can win the game, print "win", otherwise, print "Lose".
Sample Input
2 2
3 2
4 2
Sample Output
Lose
Lose
WIN
Roughly test instructions: the approximate meaning of the topic is this, that is to give you a grid of paper, size w*h, let you cut, one cut once; with the cutting, the number of paper more and more, but the size is getting smaller, who first cut out 1*1 paper who wins. This is actually the Nim game deformation, yes you did not read wrong, is the deformation of Nim game, but it has become a little big, become unrecognizable.
What is the Nim game, Daniel laughed at, said to the new contact algorithm students listen to the so-called Nim game is to give you n heap of stones, the number of A1, A2, A3,... an you choose any one from any heap, who has not won who loses. This one is the classic question, which directly gives the conclusion when satisfying the a1^a2^a3^. An==0, you lose. Is the total number of stones per heap or a value of 0 is lost.
Back to the question, how to equate with NIM game? At first there was only one piece of paper, which was equivalent to just a pile of pebbles. Here to introduce a concept of Grundy number, we now only take a bunch of stones for example, in fact, this side can be said to be the state of the transfer, a heap of x stones, once can be converted to x-1,x-2, x-3. 3, 2, 1. So the Grundy value on this side of the implied expression of a piece of paper corresponding to the number of stones, a piece of paper can be cut into two sheets of paper, the two sheets corresponding to the respective Grundy number, this is a heap of stones now actually become two piles. But the present state is actually just taking the two numbers or the values into a pile. The equivalent of the NIM game two piles of stone or can just as a bunch.
Next is the key how to calculate the Grundy number. The number of Grundy in a state is actually the smallest nonnegative integer except for the Grundy value of the state that he can move to. Why is that so? First, the stone x in the Nim heap can be converted to x-1, x-2, x-3, ... 2, 1. Set the current state Grundy number is Y, then he must be able to convert into y-1, y-2, ... 3, 2, 1. For example, for example, the current state can be converted to a Grundy number of 1, 2, 3 of the state, then the current state Grundy number must be 4, so as to be converted to the smallest. If it is 3, 5 then the current can only be 0. That is to be blunt is a number, more than this number of small number he can get through the change. Some people may have doubts, such as can be converted to 3, 5 of the Grundy value can only be 0, but this 0 can actually become 3 or 5, this does not mean that the number of stones will increase? In fact, this time the Grundy value will be larger, the back can certainly restore the original value, for what? Because each Grundy number according to his method may be can become a smaller than his arbitrary number, this does not mean that the number of stones from more and can be restored to the same.
The input w*h only as a pile of stones to deal with the corresponding Grundy value of 0 o'clock, it means to lose.
AC Code:
# include <cstdio># include <cstring># include <set>using namespace Std;int grundy[210][210];int Main ( {int W, h, I, J, K, Temp;for (i=2; i<=200; i++) {for (j=2; j<=200; J + +) {int Vis[1005];memset (VIS, 0, sizeof (VIS));//When out Now W==1 or h==1 is thought you lose will not have transferred for (k=2; i-k>=2; k++) {//enumeration can be transferred to the state vis[grundy[k][j]^grundy[i-k][j]]=1;} for (k=2; j-k>=2; k++) {//enumeration can be transferred to the transition vis[grundy[i][k]^grundy[i][j-k]]=1;} Grundy[i][j]=0;for (k=0;; k++) if (vis[k]==0) {//calculates the corresponding Grundy number grundy[i][j]=k; Break;}}} while (scanf ("%d%d", &w, &h)!=eof) {if (Grundy[w][h]) printf ("win\n"); elseprintf ("lose\n");} return 0;}
POJ Cutting Game (NIM game +grundy number)