Coin Game
Problem Description
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.
Input
The first line is a number
T
T
T(
1
<
=
T
<
=
100
1<=T<=100
1<=T<=100), represents the number of case. The next T blocks follow each indicates a case.
Each case contains two integers
N
N
N(
3
<
=
N
<
=
1
0
9
,
1
<
=
K
<
=
10
3<=N<=10^9,1<=K<=10
3<=N<=109,1<=K<=10).
Output
For each case, output the number of case and the winner “first” or “second”.(as shown in the sample output)
Sample Input
2
3 1
3 2
Sample Output
Case 1: first
Case 2: second
题意
n个硬币围成一个环,每次一方可以选择移除连续的1~k个硬币,拿走最后一个硬币的获胜。两人博弈,能否先手必胜。
题解:
第一次操作过后,则变为m个连续的硬币练成一条线,每次可以移除1~k个硬币。若需要必胜,若
k
≥
2
k\ge2
k≥2,则先手必胜(先手取中心位置的式子,将硬币分为对称的两段,对手怎么取,自己在另一段同样的位置操作,则能保证必胜)。若
k
=
=
1
k==1
k==1,则只能依次拿,当
m
%
2
=
=
0
m\%2 == 0
m%2==0时先手胜。
而n个硬币形成的环,若想要先手必胜,则需要避免上述情况的发生。综上所述,当
n
≤
k
n\le k
n≤k或
n
%
2
=
=
0
n\%2 == 0
n%2==0且
k
=
=
1
k == 1
k==1时,先手必胜。
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<ctime>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2020;
const int mod = 998244353;
int main()
{
int t, n, m, i, j, k, sig;
scanf("%d", &t);
for(i=1;i<=t;i++)
{
sig = 0;
scanf("%d %d", &n, &m);
if(m>=n)sig = 1;
else if(m == 1 && n%2)sig = 1;
printf("Case %d: %s\n", i, sig?"first":"second");
}
return 0;
}