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
 

Author
jayi

题意:

一圈长度为n的石头,每次必须连续取m个,不能取判输。


思路:

第一次取完成链,所以之后每次取都会,分解成两个子游戏。

那么枚举一条链所有分解方法,深搜这两个子游戏的sg值,取nim和,

当前长度的sg值,为所有子游戏sg值,未出现的最小非负整数。


代码:

#include<stdio.h>
#include<string.h>

int n, m, sg[1005];

int sg_dfs(int x)
{
    if(~sg[x]) return sg[x];
    if(x < m) return sg[x] = 0;

    bool book[1005] = {0};

    for(int i = 0; i <= x-m; i++)
    {
        if(i > x-m-i) break;//去重
        book[sg_dfs(i) ^ sg_dfs(x-m-i)] = 1;
    }
    for(int i = 0;; i++)
    {
        if(!book[i])
        {
            return sg[x] = i;
        }
    }
}

int main()
{
    int T, cnt = 1;
    scanf("%d",&T);
    while(T--)
    {
        memset(sg,-1,sizeof(sg));
        scanf("%d%d",&n,&m);
        if(n < m)
        {
            printf("Case #%d: %s\n",cnt++,"abcdxyzk");
            continue;
        }
        printf("Case #%d: %s\n",cnt++, sg_dfs(n-m) ? "abcdxyzk" : "aekdycoin");
    }
}

猜你喜欢

转载自blog.csdn.net/j2_o2/article/details/80423068