hdu3980 Paint Chain SG函数

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

  N个珠子,两个人轮流取连续M个,谁不能取了就输了。

  初步了解了一下SG函数,目前只会用,原理到时再研究。

  简单说就是找当前状态下一步的子状态,如果没有子状态当前sg为0,如果操作某一步后分成了多个子状态,则把他们的sg值异或,看成一个整体作为当前的一个子sg。当前sg的取值为他所有子sg里从0开始sg没有取到的那个值。

  对于这个题,就是记忆化搜索,求出当前状态的SG,判断是否为0。

  先操作一步,把环变成链,这样才能递归。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<cmath>
#define INF 0x3f3f3f3f
#define MAXN 1010
#define MAXM 20010
#define MAXNODE 4*MAXN
#define eps 1e-9
using namespace std;
int T,N,M,sg[MAXN];
int SG(int n){
    if(sg[n]!=-1) return sg[n];
    if(n<M) return sg[n]=0;
    int vis[MAXN]={0};
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=n-M-i;i++) vis[SG(i)^SG(n-M-i)]=1;
    for(int i=0;;i++) if(!vis[i]) return sg[n]=i;
}
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    int cas=0;
    while(T--){
        scanf("%d%d",&N,&M);
        printf("Case #%d: ",++cas);
        memset(sg,-1,sizeof(sg));
        if(N<M){
            printf("abcdxyzk\n");
            continue;
        }
        if(SG(N-M)) printf("abcdxyzk\n");
        else printf("aekdycoin\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值