hdu 3980 Paint Chain(SG函数)

hdu 3980 Paint Chain(SG函数)PaintChainProblemDescriptionAekdycoinandabcdxyzkareplayingagame.Theygetacirclechainwithsomebeads.Initiallynoneofthebeadsispainted.Theytaketurnstopaintthechain.InEachtur

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

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)

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

这样即可求出谁赢

代码:

#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.net/163372.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IT外包服务流程

    IT外包服务流程转载于:https://blog.51cto.com/lovezzl/93544

    2022年5月18日
    34
  • linux 脚本 ll命令,linux中ll命令的详细解释

    linux 脚本 ll命令,linux中ll命令的详细解释linxu下的ll命令其实是ls-l的一个别名。下面由学习啦小编为大家整理了linux的ll命令的详细解释的相关知识,希望对大家有帮助!一、linux中的ll命令的详细解释ll并不是linux下一个基本的命令,它实际上是ls-l的一个别名。Ubuntu默认不支持命令ll,必须用ls-l,这样使用起来不是很方便。如果要使用此命令,可以作如下修改:打开~/.bashrc找到#aliasll…

    2022年6月16日
    58
  • GTEST学习总结

    GTEST学习总结目录1.编译及学习资料1.1编译gtest1.2学习文档及资料2.gtest总结2.1gtest中的术语2.2断言2.2.1基本断言2.2.2BinaryComparison2.2.3Stringcomparison2.3创建测试用例2.4TestFixtures2.5更多断言方法2.6异常断言2.7自定义输出语句2.8…

    2022年9月29日
    4
  • css设置当字数超过限制后以省略号(…)显示

    css设置当字数超过限制后以省略号(…)显示

    2022年3月8日
    264
  • mysql左连接和右连接_MYSQL 左连接与右连接

    mysql左连接和右连接_MYSQL 左连接与右连接一、LEFTJOINLEFTJOIN关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为NULL。语法:SELECTcolumn_name(s)FROMtable1LEFTJOINtable2ONtable1.column_name=table2.column_name;举例:下面是选自”Websites”表的数据:下面…

    2022年6月4日
    33
  • python type error是什么意思_Python 报错 TypeError:’DoesNotExist’对象不可调用

    python type error是什么意思_Python 报错 TypeError:’DoesNotExist’对象不可调用公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:codedq,发送下载链接帮助你免费下载!本博客日IP超过2000,PV3000左右,急需赞助商。极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:codedq,之前的微信号好友位已满,备注:返现饿了么大量招人,我内推!Java方向!薪资不设上限,工作年龄不限…

    2025年5月23日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号