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#构造函数的作用_java中构造函数的作用

    C#构造函数的作用_java中构造函数的作用构造函数:一.构造函数的定义:二.构造函数的特点:三.构造函数的作用:四.构造函数的方式:一.构造函数的定义:构造函数:构造函数,是一种特殊的方法。主要用来在创建对象时初始化对象,即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中。特别的一个类可以有多个构造函数,可根据其参数个数的不同或参数类型的不同来区分它们即构造函数的重载,类的构造函数是类的一个特殊的成员函数,当创建类的新对象时执行。当实例化一个类对象的时候自动调用这个函数。二.构造函数的特点:特点:构造函数的命名

    2022年9月8日
    0
  • goland2021激活码【在线破解激活】

    goland2021激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    78
  • 全网解析视频接口自行测试[通俗易懂]

    全网解析视频接口自行测试[通俗易懂]http://dy.xdr630.top/v/v.php?url=http://movie.xdr630.top/jx/v.php?url=https://api.pangujiexi.com/player.php?url=http://at520.cn/jx/?url=http://player.jidiaose.com/supapi/iframe.php?v=https://jx.o…

    2022年10月24日
    0
  • 什么是draw call_unity drawcall优化

    什么是draw call_unity drawcall优化drawcall是openGL的描绘次数(directX没怎么研究,但原理应该差不多)一个简单的openGL的绘图次序是:设置颜色→绘图方式→顶点座标→绘制→结束。每帧都会重复以上的步骤。这就是一次drawcall如果有两个model,那么需要设置颜色→绘图方式→顶点座标A→绘制→结束。设置颜色→绘图方式→顶点座标B→绘制→结束。两次drawcalls;也就是说在ope

    2022年9月18日
    0
  • ios在SQLite3基本操作

    ios在SQLite3基本操作

    2022年1月1日
    39
  • webbench源码分析_webpack原理和机制

    webbench源码分析_webpack原理和机制webbench是一种网站的压力测试工具,它是由Lionbridge公司开发,wenbench的标准测试可以向我们展示两项内容:每秒钟相应的请求数和每秒钟传输的数据量;。webbench不但能具有便准静态页面的测试能力,还能对动态页面(ASP,PHP,Java,CGI)进行测试的能力。还有就是他支持对含有SSL的安全网站例如电子商务网站进行静态或动态的性能测试。Webbench最多可以模拟3

    2025年6月10日
    0

发表回复

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

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