HDU 3980 Paint Chain(SG函数)

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

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define LL long long
using namespace std;
const int MAXN = 1000 + 10;
int vis[MAXN];
int SG[MAXN];
int m;
int mex(int n)
{
    if(SG[n] != -1) return SG[n];
    if(n < m) return SG[n] = 0;
    memset(vis, 0, sizeof(vis));
    for(int i=m;i<=n;i++)
        vis[mex(i-m) ^ mex(n-i)] = 1;
    for(int i=0;;i++)
    {
        if(!vis[i])
        {
            SG[n] = i;
            break;
        }
    }
    return SG[n];
}
int main()
{
    int T, kcase = 1;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d%d", &n, &m);
        if(n < m)
        {
            printf("Case #%d: abcdxyzk\n", kcase++);
            continue;
        }
        n -= m;
        memset(SG, -1, sizeof(SG));
        for(int i=0;i<=n;i++)
            SG[i] = mex(i);
        if(SG[n] == 0) printf("Case #%d: aekdycoin\n", kcase++);
        else printf("Case #%d: abcdxyzk\n", kcase++);
    }
    return 0;
}
时间: 2024-10-29 02:50:21

HDU 3980 Paint Chain(SG函数)的相关文章

hdu 3980 Paint Chain sg函数

题目链接 给一个长度为n的环, 两个人轮流涂色, 每次涂m个连续的, 无法继续涂了就输. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(a, 0, sizeo

HDU 3980 Paint Chain(博弈 SG)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3980 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 p

hdu 3032(博弈sg函数)

题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办法求得sg的规律. 通过打表找规律可以得到如下规律:if(x%4==0) sg[x]=x-1; if(x%4==1||x%4==2) sg[x]=x; if(x%4==3) sg[x] = x+1. 打表代码: #include<iostream> #include<cstdio> #

hdu 1848 博弈之SG函数的使用

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1848 题目简单描述为: 1.  这是一个二人游戏;2.  一共有3堆石子,数量分别是m, n, p个:3.  两人轮流走;4.  每走一步可以选择任意一堆石子,然后取走f个:5.  f只能是菲波那契数列中的元素(即每次只能取1,2,3,5,8-等数量):6.  最先取光所有石子的人为胜者:假设双方都使用最优策略,请判断先手的人会赢还是后手的人会赢. 代码为: ? 1 2 3 4 5 6 7 8 9

HDU 1536 S-Nim 求SG函数

题意:给你n个数Nnum[ i ],表示每次只能取Nnum[ i ]个数. m个问题:每次给你 l 堆石子,每堆有num个石子,问先手是否会赢. Sample Input 2 2 5 3 2 5 12 3 2 4 7 4 2 3 7 12 5 1 2 3 4 5 3 2 5 12 3 2 4 7 4 2 3 7 12 0 Sample Output LWW WWL 经典Nim游戏,找出SG就可以了. 至于如何找SG,这里有详细的 点我 #include<cstdio> #include<

hdu 1729 Stone Game SG函数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729 题意:2个玩家,有N个箱子,每个箱子的大小是Si,游戏开始前,就有一些石子在这些箱子里了. 游戏者轮流选择箱子,然后把石子放入箱子里.并且放入的石子数量不能大于原来箱子里就有的石子的数量的平方. 比如说,一个箱子里已经有了3个石头,则可以放1-9个石头(小于箱子的容量). 当轮到某人时,不能再放石子则为输. 问能否找到一种策略使先手必赢. 学习了一下SG函数. 在公平的组合游戏中(游戏规则对于

HDU 5724 Chess(SG函数)

Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2605    Accepted Submission(s): 1092 Problem Description Alice and Bob are playing a special chess game on an n × 20 chessboard. There are s

hdu 1079 Calendar Game sg函数

Calendar Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2766    Accepted Submission(s): 1594 Problem Description Adam and Eve enter this year’s ACM International Collegiate Programming Con

hdu 1536&amp;&amp;1944 S-Nim sg函数

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4770    Accepted Submission(s): 2058 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now.