題目
Total Submission(s): 0 Accepted Submission(s): 0
Problem DescriptionTang 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.
InputThere 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.
OutputFor each case, print the winner's name in a single line.
Sample Input
1 130 310 20 0
Sample Output
JiangTangJiang
題解
雖然簡單的博弈,但實在是為難了我這種一直不寫博弈的渣土,無奈,手算了半天終於寫出了代碼=。=
首先解釋下題目意思,jiang和tang兩個好基友玩一個數字遊戲。給一個N,給一個k。N代表著上限,就是說兩個人在遊戲中都不能超過N;k代表著可選擇度,根據題目意思,寫數位時候,新產生的數字Y必須在老數字X的一個範圍中([1+X,k+X])。所以我們先從範例開始:
N=1,k=1:
遊戲規定,tang先開始說數字,由於遊戲規則要求數字必須大於等於1,所以tang只能報1這個數字。然而由於規則要求,誰先讓自己的數字大於等於N,那就輸了。因此,tang必輸無疑,所以勝者是
N=30,k=3:
可能會比較繞嘴,但是希望大家能慢慢看:假設jiang自己要贏這場比賽,那麼他必須逼著tang說出30這個數字。那麼毫無疑問,jiang要搶先說出29,這樣就能贏得比賽。然而從tang的角度考慮,tang不會傻到直接送對面一個勝利,所以他必須阻止jiang說出29,那麼就不能說出26這個數字,否則jiang就有機會。接著按照這個思路走下去:
jiang要搶25,tang不能說出22。
jiang要搶21,tang不能說出18。
jiang要搶17,tang不能說出14。
jiang要搶13,tang不能說出10。
jiang要搶9,tang不能說出6。
jiang要搶5,tang不能說出2。
好,停!
規律,規律!看到了沒?只要tang第一步不說2以上的數字,tang就贏了。
tang可以說“1”。
所以,tang贏了。
N=10,k=2:
還是剛才的思路:
假設jiang想贏得比賽:
jiang要搶9,tang不能說出7。
jiang要搶6,tang不能說出4。
jiang要搶3,tang不能說出1。
所以,tang不能說比1大的數字,tang輸了,jiang贏了。
N,k這樣我們就看到了一個規律,詳細的看代碼吧。程式碼範例
/*****@Polo-shen**/#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <vector>#include <map>using namespace std;#define DBG 0#define ShowLine DBG && cout<<__LINE__<<">>| "#define dout DBG && cout<<__LINE__<<">>| "#define write(x) #x" = "<<(x)<<" "#define awrite(array,num) #array"["<<num<<"]="<<array[num]<<" "#ifndef min#define min(x,y) ((x) < (y) ? (x) : (y))#endif#ifndef max#define max(x,y) ((x) > (y) ? (x) : (y))#endifint main(){ int N,k; while (cin>>N>>k && (N && k)){ int sum=N-1; int tmp=sum%(k+1); if (tmp!=0){ cout<<"Tang"<<endl; } else { cout<<"Jiang"<<endl; } } return 0;}