NIM 博弈 牛客小白月赛2 E-是是非非

题目链接

分析:一个裸的NIM博弈

对于一个Nim游戏的局面(a1,a2,...,an),它是P-position(即当前局面先手必败)当且仅当a1^a2^...^an=0,其中^表示异或(xor)运算。

一个常识:异或自己两次,相当于没有异或,即异或的消去律,可用这种方式避免T

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int inf=1<<30;
 4 typedef long long ll;
 5 const double pi=acos(-1);
 6 const int mod=1e9+7;
 7 const int maxn=1e5+1;
 8 int a[maxn];
 9 int main(){
10     int n,q,x,y;scanf("%d%d",&n,&q);
11     int sum=0;
12     for(int i=1;i<=n;i++){
13         scanf("%d",&a[i]);
14         sum^=a[i];
15     }
16     while(q--){
17         scanf("%d%d",&x,&y);
18         sum^=a[x];
19         a[x]=y;
20         sum^=a[x];
21         if(sum) cout<<"Kan
";
22         else cout<<"Li
";
23     }
24     return 0;
25 }
原文地址:https://www.cnblogs.com/qingjiuling/p/10360275.html