Nim游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nim游戏相关的知识,希望对你有一定的参考价值。

{a1,a2,a3,a4.......an}n推石头,A先拿,若a1^a2^a3^a4^.....^an==0,B胜

证明:因为异或后的数代表所有石头数在二进制表示后,对应位上有偶数个还是奇数个1;

  比如5(d)=101(b)    ,7(d)=111(b),    7^5=010(b)    ,第一个0代表7和5在这位上有偶数个0;

  所以如果异或后为0,则A进行操作后,异或不为0,B必能使异或重新变为0;最后只能B取到0(也就是拿光);

  

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
#define eps 1e-8

int main()
{    
    int n,k;
    cin>>n;
    int j=0;
    for(int i=0;i<n;i++){
        cin>>k;
        j^=k;
    }
    if(!j)cout<<"B";
    else cout<<"A";
    return 0;
}

 

以上是关于Nim游戏的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 3105 [CQOI2013]新Nim游戏 ——线性基

51nod 1069 Nim游戏 + BZOJ 1022: [SHOI2008]小约翰的游戏John(Nim游戏和Anti-Nim游戏)

Nim 游戏SG 函数游戏的和

博弈论Nim游戏:台阶集合拆分(AcWing)

BZOJ 1874: [BeiJing2009 WinterCamp]取石子游戏 [Nim游戏 SG函数]

LeetCode刷题292-简单-Nim游戏