Paint Chain
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1109 Accepted Submission(s): 388
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
Author
jayi
题意:有一串N个没有上色的佛珠,有两个人轮流给他上色,每次必须涂m个连续的佛珠,问最后不能涂下去的算输。必须要涂m个啊。我们先手肯定只能将这个串变成一个n-m个佛珠的链,然后第二个人开始涂色,每次只能涂m个。可以从(n-m-m)的任意位置开始涂,根据前面类似题目构造sg函数。sg为0代表不能涂下去了。数组定义成2048凑个整数。嗯,好像是这样。。。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
int n,m,sg[2048];
int mex(int x)
{
int i;
bool vis[2048];
memset(vis,0,sizeof(vis));
if (x-m<0) return sg[x]=0;
if (sg[x]!=-1) return sg[x];
for (i=0;i<=x-m;i++)
{
if (sg[i]==-1)
sg[i]=mex(i);
if (sg[x-i-m]==-1)
sg[x-i-m]=mex(x-i-m);
vis[sg[i] ^ sg[x-i-m]]=1;
}
for (i=0;;i++)
if (!vis[i])
{
sg[x]=i;
break;
}
return sg[x];
}
int main()
{
int T;
scanf("%d",&T);
for (int cas=1;cas<=T;cas++)
{
printf("Case #%d: ",cas);
scanf("%d%d",&n,&m);
if (m>n)
{
puts("abcdxyzk");
continue;
}
n-=m;
memset(sg,-1,sizeof(sg));
mex(n);
if (sg[n]==0) puts("aekdycoin");
else puts("abcdxyzk");
}
return 0;
}