hdu3980 Paint Chain

来源:互联网 发布:拼人脸五官的软件 编辑:程序博客网 时间:2024/06/05 09:04

Paint Chain

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 897 Accepted Submission(s): 316


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.

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
这题 要注意,如果,刚开始,总个数少于M个,那么就直接输掉,然后再解环成链,只能有一种选择,这种情况之下,就只能求出sg函数值,且先手与后手,已经交换了!
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#pragma comment(linker,"/STACK:1024000000,1024000000")#define M 1050#define mem(a,b) memset(a,b,sizeof(a))int dp[M],m;int getsg(int n){    if(dp[n]!=-1)return dp[n];    if(n<m)return dp[n]=0;    int i;bool vis[M];    mem(vis,0);    for(i=0;i<=n-m;i++){        if(dp[i]==-1)dp[i]=getsg(i);        if(dp[n-i-m]==-1)dp[n-i-m]=getsg(n-i-m);        vis[dp[i]^dp[n-i-m]]=1;    }    for(i=0;;i++)    if(!vis[i])    return i;}int main(){    int tcase,tt=1,n;    scanf("%d",&tcase);    while(tcase--){        scanf("%d%d",&n,&m);        mem(dp,-1);        printf("Case #%d: ",tt++);        int ans=0;        if(n>=m)ans=getsg(n-m)?0:1;        if(ans)        printf("aekdycoin\n");        else        printf("abcdxyzk\n");    }    return 0;}