Stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1163 Accepted Submission(s): 812
Problem Description
Tang and Jiang are good friends. To decide whose treat it is for dinner, they are playing a game. Specifically, Tang and Jiang will alternatively write numbers (integers) on a white board. Tang writes first, then Jiang, then again Tang, etc... Moreover, assuming that the number written in the previous round is X, the next person who plays should write a number Y such that 1 <= Y - X <= k. The person who writes a number no smaller than N first will lose the game. Note that in the first round, Tang can write a number only within range [1, k] (both inclusive). You can assume that Tang and Jiang will always be playing optimally, as they are both very smart students.
Input
There are multiple test cases. For each test case, there will be one line of input having two integers N (0 < N <= 10^8) and k (0 < k <= 100). Input terminates when both N and k are zero.
Output
For each case, print the winner's name in a single line.
Sample Input
1 1 30 3 10 2 0 0
Sample Output
Jiang Tang Jiang
Source
题目等价于:
一堆石子有 N 个,两个人轮流取,每次能取的范围是 [ 1 , k ] 谁最后一个取谁就输了。
Nim 的变形。
首先看下另一个问题:http://blog.youkuaiyun.com/houheshuai/article/details/44871687
这个问题将最后的条件改变了:最后取完石子的将会输。
我们先拿出一个石子放到一边,还剩 n-1 个,将这 n-1 个石子转化为上面链接中的问题,
那么先手必败的状态为: (n - 1) % ( k + 1) == 0
我们将拿出的那个石子放回去,再将游戏的最后条件变回来,那先手必败的状态仍然是:
(n - 1) % ( k + 1) == 0
代码:
#include <stdio.h>
int main()
{
int n, k;
while (~scanf("%d%d", &n, &k))
{
if (n == 0 && k == 0) break;
if ((n - 1) % (k + 1) == 0) printf("Jiang\n");
else printf("Tang\n");
}
return 0;
}