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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 系统错误&H80004005(-2147467259),未指定的错误。[通俗易懂]

    系统错误&H80004005(-2147467259),未指定的错误。[通俗易懂]系统错误&H80004005(-2147467259),未指定的错误。可能产生错误的原因:1.Flash的不断更新升级导致。2.较新版本中的MicrosoftOffice中阻止了Flash、Silverlight和Shockwave控件。解决方法一:说明:速战速决,注册表编辑启动控件,亲测可用!(缺点:可能会多编辑了一些注册表,因为是考虑了你的你电脑是32位和64…

  • DNS 全局负载均衡(GSLB)基本原理[通俗易懂]

    DNS 全局负载均衡(GSLB)基本原理[通俗易懂]采用全局负载均衡(GSLB)的前提是在不同地区设立多个数据中心,业务已经做了分布式部署的规划,无论用户从哪个IDC访问都能得到相同的结果,或者用户基本不会出现跨区域流动访问的情况,只会访问就近IDC。解析步骤1.用户向本地DNS服务器发出查询请求,如果本地DNS服务器有该域名的缓存记录,如果本地DNS服务器有该域名的缓存记录,则返回给用户,否则进行第2步2.本地DNS服务器进行递归查询,最终会查询到域名注册商处的授权DNS服务器3.授权DNS服务器其返回一条NS记录给本地DNS服务器。.

  • (授人以鱼不如授人以渔)mysql-connector-java各种版本下载地址

    (授人以鱼不如授人以渔)mysql-connector-java各种版本下载地址原文:https://blog.csdn.net/Milan__Kundera/article/details/81182757mysql-connector-java下载地址:http://mvnrepository.com/artifact/mysql/mysql-connector-java选择自己的版本:然后再点击…

  • NetCMS使用BUG记录及解决方法

    NetCMS使用BUG记录及解决方法NetCMS1.7版本使用存在两个BUG1.在上传文件时如果勾选“如果文件存在则重命名(格式:月日时5位随机数-原文件),否则将覆盖原文件.”上传的文件路径将错误。  BUG所在,NetCMS.Content.Common.UpLoad类的120行,postedFile.SaveAs(SavePath+@""+_tmps); 恩,找到了,错误就在这里。  找到了错误所在,那解决…

  • 用html设计一个静态网页_学生个人静态网页制作模板

    用html设计一个静态网页_学生个人静态网页制作模板用HTML和CSS制作简单的静态网页(小米商城)网页效果如下:代码如下(第一次写静态网页,中间css代码和html代码没有分离,所以代码可能有点乱还请见谅)1、css代码 /*——————————————–01———————————————————–*/ *{ margin:0; padding:0; } /*清除标

  • python画爱心代码大全_python爱心代码制作

    python画爱心代码大全_python爱心代码制作程序员在爱情方式上表达上展现的多种多样,其中现在大火的用编程去编写个表白内容,最受欢迎了,今天小编也尝试了下,一起来看看吧~准备工具:python3画爱心实施步骤:打开编译器,写上code,代码如下:fromturtleimport*pensize(1)pencolor(‘red’)fillcolor(‘pink’)speed(5)up()goto(-30,100)down()begin_f…

发表回复

您的电子邮箱地址不会被公开。

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