Stone(博弈)

Stone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2095    Accepted Submission(s): 1479


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

2013 ACM/ICPC Asia Regional Changchun Online


这是一道巴什博弈的变形题,巴什博奕的原意是:

A和B一块报数,每人每次报最少1个,最多报4个,看谁先报到30

其实如果知道原理,这游戏一点运气成分都没有,只和先手后手有关,比如第一次报数,A报k个数,那么B报5-k个数,那么B报数之后问题就变为,A和B一块报数,看谁先报到25了,进而变为20,15,10,5,当到5的时候,不管A怎么报数,最后一个数肯定是B报的,可以看出,作为后手的B在个游戏中是不会输的。

那么如果我们要报n个数,每次最少报一个,最多报m个,我们可以找到这么一个整数k和r,使n=k*(m+1)+r,代入上面的例子我们就可以知道,如果r=0,那么先手必败;否则,先手必胜。

 

巴什博奕:只有一堆n个物品,两个人轮流从中取物,规定每次最少取一个,最多取m个,最后取光者为胜。

而这道题目是谁先到达n谁就输,所以我们用n-1来进行巴什博奕,如果(n-1)%(k + 1)==0,说明第(n+1)/(k+1)次取完后只剩下1个,所以先取的必输,否则先取的肯定可以让在后取的去取之前把剩余的变为 一,既后取的必输


#include
using namespace std;
int main()
{
    int x,y;
    while(scanf("%d %d", &x, &y)!=EOF && (x || y)){
        if((x-1) %(y + 1)==0){
            cout << "Jiang"<< endl;
        }else cout <<"Tang"<    }
    return 0;
}

你可能感兴趣的:(博弈)