前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu 3980 Paint Chain(SG函数)

hdu 3980 Paint Chain(SG函数)

作者头像
全栈程序员站长
发布2022-09-15 10:06:28
2310
发布2022-09-15 10:06:28
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

Paint Chain

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个连续的未被染色的珠子进行染色,Aekdycoin先来,最后谁先不能染色谁则输掉了这场比赛,问最后谁会赢

思路:由于f[]数组不好求,所以要使用dfs版的SG函数

Aekdycoin先挑m个染色,则这个环就变成了一条链,那么接下来就类似于尼姆博弈了,此时abcdxyzk变成了先手

对于每一步有以下情况(现在珠子数为n-m): 前面空0个,开始染m个,则子游戏为(0,m),(n-m-m,m) 前面空1个,开始染m个,则子游戏为(1,m),(n-m-m-1,m) 前面空2个,开始染m个,则子游戏为(2,m),(n-m-m-2,m) …… 前面空n-m-m个,开始染m个,则子游戏为(n-m-m,m),(0,m)

对于每一种情况的游戏,它的结果等于其所有子游戏结果的异或。

这样即可求出谁赢

代码:

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int maxn=1e3+10;
int sg[maxn];
int n,m;

int SG_dfs(int x)
{
    if(x<m)
        return sg[x]=0;
    if(sg[x]!=-1)
        return sg[x];
    bool vis[maxn]={
  
  false};
    for(int i=0;i<=x-m;++i)
        vis[SG_dfs(i)^SG_dfs(x-m-i)]=true;
    for(int j=0;;++j)
        if(!vis[j])
    {
        sg[x]=j;
        break;
    }
    return sg[x];
}

int main()
{
    int t,k=0;
    scanf("%d",&t);
    while(++k<=t)
    {
        memset(sg,-1,sizeof(sg));
        scanf("%d%d",&n,&m);
        if(n<m)
        {
            printf("Case #%d: abcdxyzk\n",k);
            continue;
        }
        SG_dfs(n-m);
        if(sg[n-m])
            printf("Case #%d: abcdxyzk\n",k);
        else
            printf("Case #%d: aekdycoin\n",k);
    }
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/163372.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Paint Chain
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档