51nod 1714-B君的游戏(尼姆博弈)

来源:互联网 发布:gtx1070 gpu数据 编辑:程序博客网 时间:2024/06/05 12:44
1714 B君的游戏
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
 收藏
 关注
B君和L君要玩一个游戏。刚开始有n个正整数  ai  。

双方轮流操作。每次操作,选一个正整数x,将其移除,再添加7个数字  x1,x2...x7  。要求对于  xi  ,满足  0<=xi<x  且  x&xi=xi 

注意0不能被选取,所以这个游戏一定会结束,而谁无法操作谁就失败。
B君根据自己的经验,认为先手胜率高一点,所以B君是先手。
B君想知道自己是否必胜。
Input
第一行一个整数n (1 <= n <= 100000)以下n行n个数ai (0 <= a_i < 2^64)
Output
如果先手必胜,输出"B",否则输出"L"。
Input示例
41234
Output示例
B
wwwwodddd (题目提供者)
C++的运行时限为:1000 ms ,空间限制为:131072 KB 示例及语言说明请按这里

 允许其他 AC 的用户查看此代码,分享代码才能查看别人的代码并有机会获得勋章


决策其实只和每个数用二进制表示时有多少个1有关,问题转化成尼姆博弈,将每个数看做一堆石子,每个数中二进制表示1的数量表示这堆石子的数量,然后将所有石堆的sg值异或起来就好了。。。。

#include<map>   #include<stack>          #include<queue>          #include<vector>  #include<string>#include<math.h>          #include<stdio.h>          #include<iostream>          #include<string.h>          #include<stdlib.h>  #include<algorithm> #include<functional>  using namespace std;          typedef long long  ll;         #define inf  1000000000     #define mod 1000000007           #define maxn  160050#define lowbit(x) (x&-x)          #define eps 1e-9int 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 main(void){ll ans=0,x,num,tmp,n,i,j;scanf("%lld",&n);for(i=1;i<=n;i++){scanf("%lld",&x);num=0,tmp=1;for(j=0;j<=63;j++)if((tmp<<j)&x)num++;ans^=sg[num];}if(ans==0)printf("L\n");elseprintf("B\n");}


原创粉丝点击