当前位置:网站首页>博弈论 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");
}
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Appium automation test foundation - Summary of appium test environment construction
- Leetcode backtracking method
- 多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
- [leetcode] day95 effective Sudoku & matrix zeroing
- MySQL advanced part 1: stored procedures and functions
- C - XOR to all (binary topic)
- JS quickly converts JSON data into URL parameters
- LeetCode 1200. Minimum absolute difference
- leetcode-6109:知道秘密的人数
- Simple selection sort of selection sort
猜你喜欢
1.14 - 流水线
Leetcode stack related
Single chip computer engineering experience - layered idea
什么是套接字?Socket基本介绍
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
LeetCode 0107. Sequence traversal of binary tree II - another method
LVS简介【暂未完成(半成品)】
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
LaMDA 不可能觉醒吗?
随机推荐
Niu Mei's math problems
LeetCode 1200.最小绝对差
leetcode-6111:螺旋矩阵 IV
[BMZCTF-pwn] ectf-2014 seddit
11-gorm-v2-02-create data
Leetcode heap correlation
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
leetcode-31:下一个排列
SQLMAP使用教程(二)实战技巧一
LeetCode 0107. Sequence traversal of binary tree II - another method
[rust notes] 17 concurrent (Part 2)
Operator priority, one catch, no doubt
QT判断界面当前点击的按钮和当前鼠标坐标
Liunx starts redis
Data visualization chart summary (I)
1.13 - RISC/CISC
liunx启动redis
LaMDA 不可能觉醒吗?
Leetcode stack related
WordPress switches the page, and the domain name changes back to the IP address