当前位置:网站首页>博弈论 AcWing 893. 集合-Nim游戏
博弈论 AcWing 893. 集合-Nim游戏
2022-07-05 06:16:00 【T_Y_F666】
博弈论 AcWing 893. 集合-Nim游戏
原题链接
算法标签
数学知识 博弈论 SG函数
思路
若只有一堆石子 SG函数计算
结论
对于多堆石子
SG函数值等于它包含的各个子游戏SG函数值的异或和,即:SG(G) = SG(G1) ^ SG(G2) ^ … ^ SG(Gm),若SG(G)为0, 则必胜, 否则, 必败
其他概念
代码
#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 s[N], f[M];
int k, 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){
// f[x]是否已被计算
if(f[x]!=-1){
return f[x];
}else{
// 存储当前局面可以到达的局面
unordered_set<int> S;
rep(i, 0, k){
if(x>=s[i]){
S.insert(sg(x-s[i]));
}
}
// 查找集合中不存在的最小自然数
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);
k=read();
rep(i, 0, k){
s[i]=read();
}
n=read();
memset(f, -1, sizeof f);
int ans=0;
rep(i, 0, n){
int x=read();
ans^=sg(x);
}
if(ans){
puts("Yes");
}else{
puts("No");
}
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
猜你喜欢

传统数据库逐渐“难适应”,云原生数据库脱颖而出

MySQL advanced part 2: storage engine

NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar

QQ computer version cancels escape character input expression

2021apmcm post game Summary - edge detection

leetcode-6108:解密消息

RGB LED infinite mirror controlled by Arduino

LaMDA 不可能觉醒吗?

MySQL advanced part 2: MySQL architecture

SPI details
随机推荐
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Groupbykey() and reducebykey() and combinebykey() in spark
【Rust 笔记】16-输入与输出(上)
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Binary search template
剑指 Offer II 058:日程表
SPI details
Data visualization chart summary (II)
Sum of three terms (construction)
1041 Be Unique
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
MIT-6874-Deep Learning in the Life Sciences Week 7
Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
11-gorm-v2-03-basic query
Quickly use Amazon memorydb and build your own redis memory database
1039 Course List for Student
SQLMAP使用教程(一)
Sqlmap tutorial (1)
Leetcode-22: bracket generation
Overview of variable resistors - structure, operation and different applications