(轉載)SG函數模板

轉載出處:http://blog.sina.com.cn/s/blog_691ce2b70101ia1d.html

/*
計算從1-n範圍內的SG值。
Array(存儲可以走的步數,Array[0]表示可以有多少種走法)
Array[]需要從小到大排序

1.可選步數爲1-m的連續整數,直接取模即可,SG(x) = x % (m+1);
2.可選步數爲任意步,SG(x) = x;
3.可選步數爲一系列不連續的數,用GetSG(計算)
*/

int SG[MAX], hash[MAX];
void GetSG(int Array[], int n = MAX-1)
{
    int i, j;
    memset(SG, 0, sizeof(SG));
    for(i = 0; i <= n; i++)
    {
        memset(hash, 0, sizeof(hash));
        for(j = 1; Array[j]<=i; j++)
            hash[SG[i - Array[j]]] = 1;
        for(j = 0; j <= n; j++)
        {
            if(hash[j] == 0)
            {
                SG[i] = j;
                break;
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章