当前位置:网站首页>博弈论 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");
}
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 927. 三等分 模拟
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- liunx启动redis
- SQLMAP使用教程(一)
- Sum of three terms (construction)
- Flutter Web 硬件键盘监听
- Appium自动化测试基础 — Appium测试环境搭建总结
- 1.15 - input and output system
- Leetcode-9: palindromes
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
猜你喜欢

阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长

什么是套接字?Socket基本介绍

Arduino 控制的 RGB LED 无限镜

Quickly use Amazon memorydb and build your own redis memory database

1.15 - input and output system

SPI details

wordpress切换页面,域名变回了IP地址

Leetcode-6111: spiral matrix IV

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
随机推荐
高斯消元 AcWing 884. 高斯消元解异或线性方程组
【Rust 笔记】15-字符串与文本(下)
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
Record the process of configuring nccl and horovod in these two days (original)
Operator priority, one catch, no doubt
实时时钟 (RTC)
1040 Longest Symmetric String
Leetcode-1200: minimum absolute difference
Leetcode-6110: number of incremental paths in the grid graph
【Rust 笔记】16-输入与输出(下)
Leetcode divide and conquer / dichotomy
[leetcode] day95 effective Sudoku & matrix zeroing
【Rust 笔记】13-迭代器(下)
leetcode-31:下一个排列
Traversal of leetcode tree
数据可视化图表总结(一)
liunx启动redis
Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
Leetcode-6109: number of people who know secrets
MySQL advanced part 2: storage engine