码迷,mamicode.com
首页 > 其他好文 > 详细

51nod_1714:B君的游戏(博弈 sg打表)

时间:2017-02-15 16:10:36      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:span   its   控制   second   main   分配   names   cal   clu   

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1714

nim游戏的一个变形,需要打出sg函数的表

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int maxn=70000000;
int sg[65], maxs;
int vis[maxn];

//yu控制递归层数,cur控制所分配最大值,next控制所分配最小值 
void dfs(int cur, int yu, int ans, int next)
{
    if(yu==0)
    {
        vis[ans]=1;
        return ;
    }
    for(int i=next; i<cur; i++)
        dfs(cur, yu-1, ans^sg[i], i);
}
void init()
{
    sg[0]=0;
    for(int i=1;i<=64; i++)
    {
        memset(vis,0,sizeof(vis));
        dfs(i, 7, 0, 0);
        for(int j=0;;j++)
            if(!vis[j])
            {
                sg[i]=j;
                break;
            }
        printf("%d\n", sg[i]);
    }
}
void print(int n)
{
    for(int i=31;i>=0;i--)
    {
        if((n>>i)&1) cout<<1;
        else cout<<0;
    }
    cout<<endl;
}

int main()
{
    init();
    for(int i=0;i<65;i++)
        print(sg[i]);
}
//Process exited after 232.7 seconds with return value 0 
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long uLL;

int sg[] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 255, 256, 512,
            1024, 2048, 3855, 4096, 8192, 13107, 16384, 21845,
            27306, 32768, 38506, 65536, 71576, 92115, 101470,
            131072, 138406, 172589, 240014, 262144, 272069,
            380556, 524288, 536169, 679601, 847140, 1048576,
            1072054, 1258879, 1397519, 2005450, 2097152, 2121415,
            2496892, 2738813, 3993667, 4194304, 4241896, 4617503,
            5821704, 7559873, 8388608, 8439273, 8861366, 11119275,
            11973252, 13280789, 16777216, 16844349, 17102035,
            19984054, 21979742, 23734709
           };
           
int cal(uLL x)
{
    int ret=0;
    for(int i=0;i<64;i++)
        if((x>>i)&1) ret++;
    return ret;
}
int main()
{
    int n;
    while(cin>>n)
    {
        int ans=0;
        while(n--)
        {
            uLL t;
            cin>>t;
            ans ^= sg[cal(t)];
        }
        if(ans) puts("B");
        else puts("L");
    }
}

 

51nod_1714:B君的游戏(博弈 sg打表)

标签:span   its   控制   second   main   分配   names   cal   clu   

原文地址:http://www.cnblogs.com/Just--Do--It/p/6401784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!