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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • j2me开发环境搭建[通俗易懂]

    j2me开发环境搭建[通俗易懂]学习j2me的开发也有半年了,很多东西需要记住并不断实践。 j2me的环境搭建过程。 要准备的东东:1.JDK;2.开发工具Eclipse;3.eclipseMe;4.WTK;   一、下载jdk,并安装,安装好后配置环境变量,假设现在jdk的安装目录是E:/ProgramFiles/Java/jdk1.6.0_10,那么按如下配置环境变量:

    2022年7月11日
    15
  • web渗透测试工具大全_web安全攻防渗透测试实战指南 pdf

    web渗透测试工具大全_web安全攻防渗透测试实战指南 pdf一、渗透测试工具nmap,查看网站服务器开放的端口1、查看服务器上运行的服务$nmap-sVhack-test.com2、查看操作系统版本$nmap-Ohack-test.com二、使用Nikto来收集漏洞信息#官方网站:https://cirt.net/nikto2#wgethttps://cirt.net/nikto/nikto-2.1.5.

    2022年8月12日
    9
  • python调用webservice接口_webservice应用实例

    python调用webservice接口_webservice应用实例最近在搞基于python的webservice项目,今天为把环境给配好,折腾了不少时间,还是把配的过程记录下来,以后备用:首先你系统上要有python,这个不必说啦,我系统上用的是2.7+其次,要用python进行webservice开发,还需要一些库:lxml:命令行下sudoeasy_installlxml就能安装pytz:命令行下sudoeasy_installpytz就…

    2022年9月21日
    4
  • keil uvision4 注册机 使用方法「建议收藏」

    keil uvision4 注册机 使用方法「建议收藏」1.先安装keiluvision4,然后打开“File”的“LicenseManagement”拷贝CID编号。2、打开KEIL_Lic.exe,“target”选择arm,如下图所示3、把MDK4.12的CID编号粘贴到下图CID里面,点击“Generate”。4、把上图红方框内生成的注册码,拷贝到下图的“NewLicenseIDCode”内

    2022年5月20日
    126
  • android app 退出功能,Android 完美退出 App (Exit)

    android app 退出功能,Android 完美退出 App (Exit)最近两天为了解决Android上面退出程序问题折腾了半死,在google&baidu上面找了很久、很久出来的完全千篇一律,说的方法有三,但是经过我试验后全部不行。三个方法分别是:killProcess,这种方式当你kill后Activity会返回到上一个ActivityAndroidLevel8(包含8)前使用一个API来操作,Level8以后又是另外一种,所以不能通用使用…

    2022年7月17日
    21
  • 共享打印机无法连接打印,错误代码0x0000011b_打印机共享错误0x000001

    共享打印机无法连接打印,错误代码0x0000011b_打印机共享错误0x000001WIndows无法连接共享打印机,错误码:0x0000011bWin10电脑1直连的打印机,设备了共享。从另一个电脑2访问电脑1的共享打印机,连接提示错误0x0000011b,如下:经询问使用人,之前电脑2是可以正常连接到电脑1的共享打印机的,只是最近几天突然连接失败了。后得知电脑1最近有更新过系统补丁。经排查,通过卸载KB5005565补丁,重启电脑1后,电脑2成功连接到共享打印机,测试打印正常。处理过程:1.打开控制面板-程序-程序和功能-已安装更新。找到对应的KB5005565补丁,右

    2022年9月10日
    3

发表回复

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

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