题目链接:HDU3980
Paint Chain
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1803 Accepted Submission(s): 629
Problem Description
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.
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.
Input
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)
Output
For each case, print "Case #idx: " first where idx is the case number start from 1, and the name of the winner.
Sample Input
2 3 1 4 2
Sample Output
Case #1: aekdycoin Case #2: abcdxyzk
题意:一个珍珠环,每次选固定数目且没有染色过的珠子染色。最后无法染色的为输,问谁会赢。
题目分析:在环上不好考虑,第一次染色过后就相当于把环剪断了,之后每次染色都相当于把这个串剪断,之后递归使用sg函数求解即可。
//
// main.cpp
// HDU3980
//
// Created by teddywang on 2016/9/1.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int T,m,n;
int sg[2006];
int mex(int a)
{
if(sg[a]!=-1) return sg[a];
int vis[2001];
memset(vis,0,sizeof(vis));
for(int i=0;2*i<=a-m;i++)
{
sg[i]=mex(i);
sg[a-m-i]=mex(a-m-i);
vis[sg[i]^sg[a-m-i]]=1;
}
for(int i=0;i<n-m;i++)
{
if(!vis[i]) return i;
}
return -1;
}
int main()
{
cin>>T;
for(int k=1;k<=T;k++)
{
cin>>n>>m;
memset(sg,-1,sizeof(sg));
sg[0]=0;
if(n>m) sg[n-m]=mex(n-m);
printf("Case #%d: ",k);
if(n>=m&&sg[n-m]==0)
printf("aekdycoin\n");
else
printf("abcdxyzk\n");
}
}