博弈论 AcWing 894. 拆分-Nim游戏
原题链接
算法标签
数学知识 博弈论 SG函数
思路
代码
#include<bits/stdc++.h>
#define int long long
#define abs fabs
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 105, M = 10005;
int f[M];
int n;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
int sg(int x){
if(f[x]!=-1){
return f[x];
}else{
unordered_set<int> S;
// 第一堆数量要求
rep(i, 0, x){
// 第二堆数量要求
rep(j, 0, i+1){
S.insert(sg(i)^sg(j));
}
}
// mex 函数
for(int i=0;;++i){
if(!S.count(i)){
return f[x]=i;
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(f, -1, sizeof f);
n=read();
int ans=0;
while(n--){
int x=read();
ans^=sg(x);
}
if(ans){
puts("Yes");
}else{
puts("No");
}
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈