hdu3980Paint Chain+SG博弈

来源:互联网 发布:org.apache jar包 编辑:程序博客网 时间:2024/06/07 09:17

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

Author
jayi

Source
2011 Multi-University Training Contest 14 - Host by FZU

一个圆环N个数,每次每个人只能给连续的M个数抹漆,最后不能抹的就算输(大概这个意思啦,英语不好)。。首先第一个人抹了之后就变成直线了。。先手变后手了..
(5,2) +++++之后的状态可以有<–+++><+–++><++–+><+++–>
一个问题的结果是其子问题的结果的亦或。。。
对于一个直线的情况,其子状态有前边空几个那么多的情况。。
比如(5,2) +++++,
当前边空0个时子游戏为(0,2)(3,2)
当前边空1个时子游戏有(1,2)(2,2)
当前边空2个时子游戏有(2,2)(1,2)
当前边空3个时子游戏有(3,2)(0,2)
所以求SG时记忆化递推。。就可以求出(n,m)状态的SG。

好了贴代码。。

#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<cstdlib>#include<queue>#include<vector>#include<map>#include<stack>#include<set>using namespace std;typedef long long LL;int SG[1005];int getSG(int n,int m){    if(n<m) return SG[n]=0;    if(SG[n]!=-1) return SG[n];    bool visit[1005];// //下边递归了。。不要放全局去。。。    memset(visit,false,sizeof(visit));    for(int i=0;i<=n-m;i++){        visit[getSG(i,m)^getSG(n-m-i,m)]=true;    }    for(int i=0;;i++){        if(visit[i]==false){            SG[n]=i;            break;        }    }    return SG[n];}int main(){    int t;    scanf("%d",&t);    for(int test=1;test<=t;test++){        int n,m;        scanf("%d %d",&n,&m);        memset(SG,-1,sizeof(SG));        printf("Case #%d: ",test);        if(n<m||getSG(n-m,m)) printf("abcdxyzk\n");        else printf("aekdycoin\n");    }    return 0;}/*23 1*/
0 0
原创粉丝点击