nyoj 913 take stones (ten)

sg function to play table

//f[]: The number of stones that can be removed
//sg[]: 0~n SG function value
//hash[]:mex{}
int f[N],sg[N],hash[N];     
void getSG(int n)
{
    int i,j;
    memset(sg,0,sizeof(sg));
    for(i=1;i<=n;i++)
    {
        memset(hash,0,sizeof(hash));
        for(j=1;f[j]<=i;j++)
            hash[sg[i-f[j]]]=1;
        for(j=0;j<=n;j++) //find the smallest non-negative integer that does not appear in mes{}
        {
            if(hash[j]==0)
            {
                sg[i]=j;
                break;
            }
        }
    }
}
dfs
//Note that the S array needs to be sorted from small to large. The SG function needs to be initialized to -1. For each collection, it only needs to be initialized once
//n is the size of the set s S[i] is an array of defined special retrieval rules
int s[110],sg[10010],n;
int SG_dfs(int x)
{
    int i;
    if(sg[x]!=-1)
        return sg[x];
    bool fish [110];
    memset(vis,0,sizeof(vis));
    for(i=0;i<n;i++)
    {
        if(x>=s[i])
        {
            SG_dfs(x-s[i]);
            vis[sg[x-s[i]]]=1;
        }
    }
    you are;
    for(i=0;;i++)
        if(!vis[i])
        {
            e=i;
            break;
        }
    return sg[x]=e;
}

Calculate the SG function of each pile separately, you can use the conclusion of the combined game to perform Nim XOR

The first pile of SG functions can be found to have regular 0, 1, 2, 0, 1, 2...cycles

The second pile is irregular to find sg2[]

The third stack sg[a]=a;

The fourth pile is irregular to find sg4[]

The fifth pile of rules 0, 1, 0, 1, 0, 1...

The sixth pile and beyond According to the Bash game, sg[a]=a%(i+1);

#include<stdio.h>
#include<string.h>
int f[25],sg2[1005],vis[1005];
int ff[1000],sg4[1005];

void fib()//Fibonacci number
{
    f[1]=1;
    f[2]=2;
    for(int i=3; i<=20; i++)
        f[i]=f[i-1]+f[i-2];
    return ;

}
void get_ff()//1 and even numbers
{
    ff[1]=1;
    for(int i=1; i<=500; i++)
    {
        ff [i + 1] = 2 * i;
    }
}
int get_sg2(int n)//The second heap of sg
{
    fib();
    memset(sg2,0,sizeof(sg2));
    for(int i=1; i<=n; i++)
    {
        memset(vis,0,sizeof(vis));
        for(int j=1; f[j]<=i; j++)
            vis[sg2[i-f[j]]]=1;
        for(int j=0; j<=n; j++)
            if(vis[j]==0)
            {
                sg2[i]=j;
                break;
            }
    }
    return sg2[n];
}
int get_sg4(int n)//The fourth heap sg
{
    memset(sg4,0,sizeof(sg4));
    get_ff();
    for(int i=1; i<=n; i++)
    {
        memset(vis,0,sizeof(vis));
        for(int j=1; ff[j]<=i; j++)
            vis[sg4[i-ff[j]]]=1;
        for(int j=0; j<=n; j++)
            if(vis[j]==0)
            {
                sg4[i]=j;
                break;
            }
    }
    return sg4[n];
}
intmain()
{
    int m,n,p,a;
    while(~scanf("%d",&n)&&n)
    {
        int f=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a);
            if(i==1)
            {
                f^=(a%3);
            }
            else if(i==2)
            {
                f^=get_sg2(a);
            }
            else if(i==3)
            {
                f^=a;
            }
            else if(i==4)
            {
                f^=get_sg4(a);
            }
            else if(i==5)
            {
                f^=(a%2);
            }
            else
            {
                f^=(a%(i+1));
            }
        }
        if(f>0)
        {
            printf("Yougth\n");
        }
        else
        {
            printf("Hrdv\n");
        }
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325888661&siteId=291194637