(HDU 3980)Paint Chain (Nim博弈,环变链)

Paint Chain

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3980
Time Limit: 2000/1000 MS (Java/Others)

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个连续的珠子,最后涂不了的算输

n元环可以经过去一次之后变成n-m元链,链可以用Nim和来计算sg值
因为可以这样理解
对于n-m元链,每次选择一个位置涂上m个,把剩余部分分成两份(包括0),
也就是游戏被分成了两个子游戏,两个子游戏构成一种子局面,染m的位置有多种,该状态的的SG值由多种子局面的SG值决定。
不过需要注意的一点是取了m个之后变成n-m元链,要转换先后手!

#include<bits/stdc++.h>
using namespace std;
#define maxn 1111
int n,m;
bool vis[maxn];
int sg[maxn];
//打表做法
void getsg()
{
    for(int i=0;i<m;i++)
        sg[i]=0;
    sg[m]=1;
    for(int i=m+1;i<=n;i++)//n>m(i>=m)
    {
        memset(vis,0,sizeof(vis));
        for(int j=0;j<i-m;j++)
        {
            vis[sg[j]^sg[i-m-j]]=1;
        }

        for(int j=0;;j++)
        if(!vis[j]){sg[i]=j;break;}
    }
    return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t,cas=0;
    cin>>t;
    while(t--)
    {
        cas++;
        cin>>n>>m;
        getsg();
        /*for(int i=0;i<=n;i++)
            cout<<sg[i]<<endl;*/
        printf("Case #%d: ",cas);
        if(n>m&&!sg[n-m]||n==m)puts("aekdycoin");//n>m的时候而且sg[n]=0时表示后手(解成链后后手变先手)必败即先手胜
        else
            puts("abcdxyzk");//n<=m时先手败,n>m时sg[n-m]=1也是先手败
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值