題解報告:hdu 4764 Stone(巴什博弈)
阿新 • • 發佈:2018-05-28
玩遊戲 per content 超過 c++ 寫入 lse note write 為了決定吃晚餐的人,他們正在玩遊戲。具體來說,唐和江會在白板上寫數字(整數)。唐先寫,然後寫江,然後再寫唐,等等......此外,假設前一輪寫的數字是X,下一個玩的人應該寫一個數Y,使得1 <= Y - X <= k 。首先寫入不小於N的數字的人將失去遊戲。請註意,在第一輪中,唐可以只在範圍[1,k](包括兩端)內編寫一個數字。你可以認為唐和姜總是會打得最好,因為他們都是非常聰明的學生。
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.
譯文:有多個測試用例。 對於每個測試案例,將有一行輸入具有兩個整數N(0 <N <= 10 ^ 8)和K(0 <K <= 100)。當N和k都為零時輸入終止。
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
解題思路:簡單的巴什博弈。題目的意思就是誰先寫到數字N或者是超過N誰就輸,換句話說,誰只要所選數字到達n-1,誰就贏。規則是先手剛開始只能在[1,k]中編寫一個數字,即選擇一個不大於k的數字,接下來的規則就是1<=Y-X<=k,我們可以發現,其實Y就是對之前兩個人所選數字的累加和,也就是之後輪到的人所選擇的數字至少為1,至多為k。到這,所有條件已經滿足巴什博弈的模型了。我們將問題轉換一下,現有N-1這個和數,要求每次選擇減去一個不大於k的數字,誰最後減去一個數字後和數變為k+1,誰就贏,因為接下來的人選擇減去的數字不超過k,即最後和數剩下不超過k,再輪到的人都能一次減掉剩下的和數,即此時輪到的人必勝。
結論:當(n-1)%(k+1)==0,後手“Jiang”必贏,否則先手“Tang”必贏。
AC代碼:
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4764
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. 譯文:唐和江是好朋友。1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int n,k; 6 while(cin>>n>>k && (n+k)){ 7 if((n-1)%(k+1)==0)cout<<"Jiang"<<endl;//後手必贏 8 else cout<<"Tang"<<endl;//先手必贏 9 } 10 return 0; 11 }
題解報告:hdu 4764 Stone(巴什博弈)