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


相关推荐

  • 香农编码c++实现_香农费诺编码例子

    香农编码c++实现_香农费诺编码例子实验三香农编码信息论与编码实验报告院系:哈尔滨理工大学荣成校区专业:电子信息工程学号:姓名:日期:2015年6月16日香农编码信息论与编码第三次实验报告一、实验目的和任务?1、?理解信源编码的意义;?2、?熟悉?MATLAB程序设计;??3、?掌握香农编码的方法及计算机实现;??4、?对给定信源进行香农编码,并计算编码效率;?二、实验原理介绍?给定某个信源符号的概率分布,通过以下的步骤进行香…

    2022年9月10日
    0
  • WordPress建站_如何建设社区

    Wordpress建站_如何建设社区创始人或建设者,您需要了解将社区集成到您的项目中的哪些内容?采用什么样的去中心化的方式?”在2020年之前,我花了很多时间分析中心化社区建设最佳实践的来龙去脉,主要来自我作为用户(Foursquare、Meetup、Twitter),作为一名员工(StackOverflow),或者在我在UnionSquareVentures期间通过渗透了解到的成功Web2网络示例。…

    2022年10月2日
    0
  • mipi 协议简介[通俗易懂]

    mipi 协议简介[通俗易懂]转载于:https://blog.csdn.net/g_salamander/article/details/9163455以下是最近几个月在调试 MIPIDSI/CSI 的一些经验总结,因为协议有专门的文档,所以这里就记录一些常用知识点:一、D-PHY1、传输模式LP(Low-Power)模式:用于传输控制信号,最高速率10MHzHS(High-Speed)模式:用于高速传输数据,速…

    2022年4月30日
    237
  • 查找历史市盈率

    查找历史市盈率https://www.zhihu.com/question/28424413

    2022年8月3日
    3
  • 关于might_sleep的一点说明【转】

    关于might_sleep的一点说明【转】

    2022年3月5日
    29
  • 系统无法开始服务器进程。请检查用户名和密码。 (Exception from HRESULT: 0x8000401A)…[通俗易懂]

    系统无法开始服务器进程。请检查用户名和密码。 (Exception from HRESULT: 0x8000401A)…[通俗易懂]开始-运行-cmd,输入aspnet_regiis.exe-i重新注册iis或者出现以下错误:检索COM类工厂中CLSID为{000209FF-0000-0000-C000-000000000046}的组件失败,原因是出现以下错误:8000401a因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。(异常来自HRESULT:0x8000401A)。解决方案:1….

    2022年8月20日
    16

发表回复

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

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