CF 317D Game with Powers

题解:

  将一个数的指数看着一个堆,问题变成这些堆的异或值

  分析一个堆的情况,打SG表。

#include<stdio.h>
#include<string.h>
char sg[1100000000];

char getsg(int x)
{
    if(sg[x]>=0) return sg[x];
    bool hash[20]= {0};
    for(int i=0; i<30; i++)
    {
        if(x&(1<<i))
        {
            int t=i+1,tmp=x;
            while(t<=30)
            {
                if(tmp&(1<<(t-1))) tmp-=1<<(t-1);
                t+=i+1;
            }
            hash[getsg(tmp)]=1;
        }
        //printf("%d %d
",i,getsg((1<<i)-1));
    }
    int i=0;
    while(hash[i]) i++;
    return sg[x]=i;
}

int main()
{
    memset(sg,-1,sizeof(sg));
    sg[0]=0;
    int i=30;
    getsg((1<<i)-1);
    for(int i=1; i<=30; i++)
    {
        printf("%d %d
",i,sg[(1<<i)-1]);
    }
    return 0;
}
打表找规律
原文地址:https://www.cnblogs.com/XDJjy/p/3365881.html