当前位置:网站首页>Nim游戏阶梯 Nim游戏和SG函数应用(集合游戏)
Nim游戏阶梯 Nim游戏和SG函数应用(集合游戏)
2022-06-13 10:57:00 【重生之我会拧瓶盖】
阶梯Nim游戏
现在,有一个 n 级台阶的楼梯,每级台阶上都有若干个石子,其中第 i 级台阶上有 ai 个石子(i≥1)。
两位玩家轮流操作,每次操作可以从任意一级台阶上拿若干个石子放到下一级台阶中(不能不拿)。
已经拿到地面上的石子不能再拿,最后无法进行操作的人视为失败。
问如果两人都采用最优策略,先手是否必胜。
输入格式
第一行包含整数 n。
第二行包含 n 个整数,其中第 i 个整数表示第 i 级台阶上的石子数 ai。
输出格式
如果先手方必胜,则输出 Yes。
否则,输出 No。
数据范围
1≤n≤105,
1≤ai≤109
输入样例:
3
2 1 3
输出样例:
Yes
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
int res = 0;
for (int i = 1; i <= n; i ++ ){
int x;
cin>>x;
if (i%2) res^= x;
}
if (res) cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
集合Nim游戏
给定 n 堆石子以及一个由 k 个不同正整数构成的数字集合 S。
现在有两位玩家轮流操作,每次操作可以从任意一堆石子中拿取石子,每次拿取的石子数量必须包含于集合 S,最后无法进行操作的人视为失败。
问如果两人都采用最优策略,先手是否必胜。
输入格式
第一行包含整数 k,表示数字集合 S 中数字的个数。
第二行包含 k 个整数,其中第 i 个整数表示数字集合 S 中的第 i 个数 si。
第三行包含整数 n。
第四行包含 n 个整数,其中第 i 个整数表示第 i 堆石子的数量 hi。
输出格式
如果先手方必胜,则输出 Yes。
否则,输出 No。
数据范围
1≤n,k≤100,
1≤si,hi≤10000
输入样例:
2
2 5
3
2 4 7
输出样例:
Yes
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>
using namespace std;
const int N = 1e4+10;
int a[110],f[N];
int n,k;
int SG (int x)
{
if (f[x]!=-1) return f[x];
unordered_set<int> mh;
for (int i=0;i<k;i++)
{
int j = a[i];
if (x>=j) mh.insert(SG(x-j));
}
for (int i=0;;i++)
if (!mh.count(i)) return f[x]=i;
}
int main(){
ios::sync_with_stdio(false);
cin>>k;
for (int i = 0; i < k; i ++ ) cin>>a[i];
cin>>n;
memset(f, -1, sizeof f);
int res = 0;
while (n -- )
{
int x;
cin>>x;
res^=SG(x);
}
if (res) cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
拆分-Nim游戏
给定 n 堆石子,两位玩家轮流操作,每次操作可以取走其中的一堆石子,然后放入两堆规模更小的石子(新堆规模可以为 0,且两个新堆的石子总数可以大于取走的那堆石子数),最后无法进行操作的人视为失败。
问如果两人都采用最优策略,先手是否必胜。
输入格式
第一行包含整数 n。
第二行包含 n 个整数,其中第 i 个整数表示第 i 堆石子的数量 ai。
输出格式
如果先手方必胜,则输出 Yes。
否则,输出 No。
数据范围
1≤n,ai≤100
输入样例:
2
2 3
输出样例:
Yes
666
相比于集合-Nim,这里的每一堆可以变成不大于原来那堆的任意大小的两堆
即a[i]a[i]可以拆分成(b[i],b[j])(b[i],b[j]),为了避免重复规定b[i]>=b[j]b[i]>=b[j],即:a[i]>=b[i]>=b[j]a[i]>=b[i]>=b[j]
相当于一个局面拆分成了两个局面,由SG函数理论,多个独立局面的SGSG值,等于这些局面SGSG值的异或和。
因此需要存储的状态就是sg(b[i])sg(b[i])^sg(b[j])sg(b[j])(与集合-Nim的唯一区别)
#include <iostream>
#include <cstring>
#include <algorithm>
#include<unordered_set>
using namespace std;
const int N = 110;
int a[N],f[N];
int n;
int sg(int x)
{
if (f[x]!=-1) return f[x];
unordered_set<int> mh;
for (int i=0;i<x;i++)
for (int j=0;j<=i;j++)
mh.insert(sg(i)^sg(j));
for (int i=0;;i++)
if (!mh.count(i)) return f[x] = i;
}
int main()
{
ios::sync_with_stdio(false);
memset(f, -1, sizeof f);
cin>>n;
int res = 0;
for (int i = 0; i < n; i ++ ) {
cin>>a[i];
res^= sg(a[i]);
}
if (res) cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
边栏推荐
- 2022 tailings recurrent training question bank and simulated examination
- Solution to qt5.12 unable to input Chinese (unable to switch Chinese input method) in deepin system
- CommonAPI与AUTOSAR AP通讯管理的异同
- 21世纪以来的历次“粮食危机”,发生了什么?
- 宝塔访问从IP改为域名
- 2022年劳务员-通用基础(劳务员)上岗证题目及答案
- 2022年劳务员-通用基础(劳务员)上岗证题目及答案
- D generate unique ID at compile time
- Database learning notes (Chapter 15)
- 2022煤矿探放水特种作业证考试题库模拟考试平台操作
猜你喜欢
Go zero microservice Practice Series (III. API definition and table structure design)
恶意代码实战分析Lab05-01
Use of servers
EasyClick 运行代码片段出Null
2021CCPC网络赛题解加总结
Vivo large scale kubernetes cluster automation operation and maintenance practice
ST表学习
spark源码(一)spark-submit如何将jar以及配置参数提交给spark服务器
Do you agree that the salary of hardware engineers is falsely high?
第七章 文件管理作业
随机推荐
Go zero microservice Practice Series (III. API definition and table structure design)
Easyclick run code snippet out null
Pytorch basis (II) -- tensor and gradient
ACP | 东北地理所在气象-空气质量双向耦合模式研究中取得进展
Advanced technology management - what management tools can managers use
2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
C# 文件打包下载
2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
什么是400G以太网?
Understand an article: Spark operation mode
Folder data synchronization tool sync folders Pro
报告录屏+PPT 傅云飞-喜马拉雅山脉南坡云降水特征研究
Vivo large scale kubernetes cluster automation operation and maintenance practice
[elm classification] data classification based on particle swarm optimization convolution neural network CNN combined with limit learning machine elm with matlab code
SSM integration preliminary details
Introduction to binary tree
第七章 文件管理作业
Nature communications - modeling armed conflict risk under climate change using machine learning and time series data
第六章 I/O管理作业
Brief description of redo logs and undo logs in MySQL