After hh has learned how to play Nim game, he begins to try another coin game which seems much easier.
The game goes like this:
Two players start the game with a circle of n coins.
They take coins from the circle in turn and every time they could take 1~K continuous coins.
(imagining that ten coins numbered from 1 to 10 and K equal to 3, since 1 and 10 are continuous, you could take away the continuous 10 , 1 , 2 , but if 2 was taken away, you couldn't take 1, 3, 4, because 1 and 3 aren't continuous)
The player who takes the last coin 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.
The first line is a number T(1<=T<=100), represents the number of case. The next T blocks follow each indicates a case.
Each case contains two integers N(3<=N<=10
9,1<=K<=10).
For each case, output the number of case and the winner "first" or "second".(as shown in the sample output)
Case 1: first Case 2: second
題目意思:對於t組樣例,有n硬幣,編好號,組成環,每一次能夠連續的取k個,誰最後取完誰贏。
解題思路:這是一道博弈問題,遊戲剛開始的時候全部石子爲一條環,先手不可能一次所有取完的狀況下,石子就會變成一條鏈,然然後手只須要建立一個對稱的局勢就能夠取得勝利,就是把這條鏈分紅兩條相等的鏈。咱們發現當k=1時,對稱局勢對遊戲沒有任何影響,勝負取決於奇偶性;而k>=2時,後手利用對稱局勢能夠取得勝利,先手必輸。
#include<stdio.h>
int main()
{
int t,i,k,n,flag;
scanf("%d",&t);
i=1;
while(t--)
{
scanf("%d%d",&n,&k);
if(k>=n)//先手勝利
{
flag=0;
}
else if(k==1)
{
if(n%2==1)///奇數先手必勝
flag=0;
else
flag=1;///偶數後手對稱拆,必勝
}
else
flag=1;///k>1時後手利用對稱局勢,必勝
if(flag==0)
printf("Case %d: first\n",i);
else
printf("Case %d: second\n",i);
i++;
}
return 0;
}