HDU3980 Paint Chain SG函数

20 篇文章 0 订阅

题目链接: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.
 

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");
    }
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值