A simple stone game
| Time Limit:1000 MS |
|
Memory Limit:65536 K |
| Total Submissions:310 |
|
Accepted:161 |
Description
After he has learned how to play Nim game, Mike begins to try another stone game which seems much easier.
The game goes like this: Two players start the game with a pile of n stones. they take stones from the pile in turn and every time they take at least one stone. the one who goes first can take at most n-1 stones for his first move. from then on a player can take at most k times as your stones as his opponent has taken last time. for example, if one player takes m stones in his turn, then the other player can take at most k * m stones next time. the player who takes the last stone wins the game. suppose that those two players always take the best moves and never make mistakes, your job is to find out who will definitely win the game.
Input
The first line contains a integer t, indicating that there are t test cases following. (t <= 20 ).
Each test case is a line consisting of two integer n and k. (2 <= n <= 105, 1 <= k <= ).
Output
For each test case, output one line starting with "Case N:", N is the case number. and then, if the first player can ensure a winning, print the minimum number of stones he shoshould take in his first turn. otherwise, print "lose ". please note that there is a blank following the colon.
Sample Input
5 16 1 11 1 32 2 34 2 19 3
Sample Output
Case 1: loseCase 2: 1Case 3: 3Case 4: loseCase 5: 4
Ideas:
The K-times dynamic subtraction game in game theory is difficult. I am confused only by reading a lot of materials!
This question can be seen as the expansion of the Fibonacci game, it is recommended that do not understand the first learning of the Fibonacci game, personal finishing http://blog.csdn.net/tbl_123/article/details/24033245;
The extension is embodied in the fact that the series is no longer a Fib series, which is self-constructed based on the value of k. The specific construction method is as follows:
Here is a simple description. First, we will discuss the situation based on k values:
1) When k = 1, the defeat state is n = 2 ^ I, because after we break down the number in binary format, we take away the last 1 in binary format, then the other party must not take the second-to-last one, because he cannot take more than you. As long as you follow this rule, the other party will never be able to finish it. So you will win. However, when there is only one shard in the binary, because the first runner cannot finish all the shards, the latter must be able to get the last one!
For example, when n = 6 = (110:
First round: the first time the first hand gets the rightmost 1, that is, 2, there are 4 (100) remaining, and the last hand can take 1 or 2;
In the second round: If two are obtained after the last round, the first two are directly won.
If the latter hand gets one, there are three left. You can only get one, and you can only get one in the future, so you must win!
2) When k = 2, the naked Fibonacci game is over. Here we will not talk about it more clearly. In fact, after being disassembled, n can also be expressed in binary format and understood using k = 1. For example, n = 11 = 7 + 3 + 1 can be expressed as 10101;
3) When k gets any non-zero positive value, the key points are:
Just like in the Fibonacci game, we first need a series to break n into the sum of some items in the series, and then we can complete it according to the solution of the Fibonacci game, you can also use the binary method to understand that the last 1 is still consistent with the preceding condition.
We use array a to represent the number of columns to be evaluated. array B [I] saves the maximum number that can be constructed by a [0... I] combination. It is a bit difficult to understand here. The so-called structure refers to the inverse process in which n is decomposed into Fib numbers. For example, when k = 2, a [N] = {1, 2, 3, 5, 8, 13, 21, 33 ....} (Fibonacci array); then B [3] is the largest number that can be constructed by 1, 2, and 3. The answer is 4, which is a bit incredible? You may ask why it is not 5, 6, or anything else. In fact, 4 can be divided into 1 + 3 without dispute, but can 5 be divided into 2 + 3? No, because 5 is also the number of Fibonacci; 6 can be broken down, but not 1 + 2 + 3, but 1 + 5.
After the above, we know that B [I] is a [0... i] the maximum number that can be constructed, then a [I + 1] = B [I] + 1; because a array (Fib array) the stored numbers are not constructible (it is a defeat to obtain them). Obviously, a [0... i] the maximum number + 1 is the next unconstructible number (a [I + 1]).
Then, for the calculation of B [I], since it is a [0... i] to construct the largest number, a [I] must be selected (here we need some reasoning. When a [I] constructs a number, the adjacent j numbers cannot be used at the same time, just as the 2 and 3 numbers cannot be constructed as 5, and the reasoning should be completed by yourself), then the next item to be selected can only be searched in descending order, until a [t] satisfies a [t] * K <a [I], and B [t] is a [0... t] the maximum number that can be constructed, plus a [I], that is, a [0... i] the largest number that can be constructed, So B [I] = B [t] + a [I].
After the series of results, the subsequent work will be simple. It is the same as that in the Fibonacci game. If n = The number in the series, it will be defeated; otherwise, it will win; when it wins, it is required to output the first step to obtain the method, actually, it is the smallest of the decomposed series. See the code.
Code:
# Include
# Define N 20000005int a [N], B [N]; // a is a series, and B stores a [0... i] int main () {int n, k; int loop = 0, casei = 1; scanf ("% d", & loop ); while (loop --) {scanf ("% d", & n, & k); a [0] = B [0] = 1; int I = 0, j = 0; while (n> a [I]) {// construct the sequence I ++; a [I] = B [I-1] + 1; while (a [j + 1] * k <a [I]) j ++; if (k * a [j] <a [I]) B [I] = B [j] + a [I]; elseb [I] = a [I];} printf ("Case % d:", casei ++ ); if (n = a [I]) printf ("lose \ n"); else {int ans; while (n) {if (n> = a [I]) {// number in the smallest series of n, that is, the number n-= a [I]; ans = a [I];} I --;} printf ("% d \ n", ans) ;}} return 0 ;}