【题解】HDU-3980 Paint Chain

Paint Chain(HDU-3980)

题面

Aekdycoin and abcdxyzk are playing a game. They get a circle chain with some beads. Initially none of the beads is painted. They take turns to paint the chain. In Each turn one player must paint a unpainted beads. Whoever is unable to paint in his turn lose the game. Aekdycoin will take the first move.

Now, they thought this game is too simple, and they want to change some rules. In each turn one player must select a certain number of consecutive unpainted beads to paint. The other rules is The same as the original. Who will win under the rules ?You may assume that both of them are so clever.

输入

First line contains T, the number of test cases. Following T line contain 2 integer N, M, indicate the chain has N beads, and each turn one player must paint M consecutive beads. (1 <= N, M <= 1000)

输出

For each case, print "Case #idx: " first where idx is the case number start from 1, and the name of the winner.

样例输入

1
2
3
2
3 1
4 2

样例输出

1
2
Case #1: aekdycoin
Case #2: abcdxyzk

提示

思路

Anti-Nim博弈,可以参见【题解】POJ-3480 John

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using namespace std;
int SG[1005];

int getSg(int n, int m){
if(n<m) return SG[n]=0;
if(SG[n]!=-1) return SG[n];
bool S[1005]={0};

for(int i=0; i<=n-m; i++)
S[getSg(i, m) ^ getSg(n-m-i, m)] = 1;

int mex = 0;
while(S[mex]) mex++;
return SG[n]=mex;
}

int main()
{
int T; scanf("%d", &T);
for(int cs=1; cs<=T; cs++)
{
memset(SG, -1, sizeof(SG));
int n, m;
scanf("%d %d", &n, &m);
printf("Case #%d: ", cs);
if(n<m || getSg(n-m, m)){
printf("abcdxyzk\n");
}else{
printf("aekdycoin\n");
}
}
return 0;
}