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)
上一篇 2022年7月23日 下午7:46
下一篇 2022年7月23日 下午7:46


相关推荐

  • MacBook安装telnet工具和使用

    MacBook安装telnet工具和使用MacBook 安装 telnet 工具和使用 telnet 安装使用 brew 工具安装 brewinstallt 工具使用 ping 命令是测试本机和远程机器的 ip 通不通 telnet 工具可以测试本机和远程机器的端口通不通 pingIP 为 192 168 3 2 通不通 ping192 168 3 2 测试 192 168 3 2 机器的 1883 端口是否通 telnet192 168 3 21883

    2026年3月18日
    3
  • 二、第一个java程序:HelloWorld

    二、第一个java程序:HelloWorld前面讲解了java程序的配置,现在要开始进入实例的编程了,第一个程序还是沿用经典的HelloWorld程序进行讲解。一、编程源代码     打开记事本,输入以下代码:publicclassHelloWorld{     //程序的主函数入门     publicstaticvoidmain(Stringargs[])

    2022年5月28日
    38
  • Hunyuan-MT-7B部署教程:vLLM显存优化技巧让7B模型在24G GPU运行

    Hunyuan-MT-7B部署教程:vLLM显存优化技巧让7B模型在24G GPU运行

    2026年3月13日
    2
  • pip升级所有库(包)

    pip升级所有库(包)前言使用 pip 工具管理第三方库 升级方式 确定 pip 版本 如果使用的 pyton2 也就是默认的 python 那么 pip 就使用默认的 pip 如果使用的 python3 那么 pip 也需要使用 pip3 我使用的是 python3 pip3 pip 版本首先确保 pip 的版本是可用的 确保升级库过程中不会报错 查看 pip 版本 pip3version 如果在安装库的过程中 出现以下提示说明 pip 版本过低 需要升级了 WARNING Youareusingp 2 4 h

    2026年3月18日
    2
  • 什么是AI Agent应用的软件平台?2025年5大主流平台深度对比分析

    什么是AI Agent应用的软件平台?2025年5大主流平台深度对比分析

    2026年3月16日
    2
  • php禅道安装,(推荐)windows用一键安装包安装

    php禅道安装,(推荐)windows用一键安装包安装为了简化大家在 windows 下面的安装 我们在 xampp 基础上做了禅道的 windows 一键安装包 xampp 是业内非常著名的 AMP 集成运行环境 禅道的一键安装包主要在它基础上做了大量的精简 并集成了我们自主开发的集成面板 使用起来会更加方便 关于 xampp 一键安装包 大家有兴趣可以访问下面的官方网站 https www apachefriend org 注 这个是 xampp 官方网站 禅道

    2026年3月17日
    1

发表回复

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

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