当前位置:网站首页>Nim game ladder Nim game and SG function application (set game)
Nim game ladder Nim game and SG function application (set game)
2022-06-13 11:02:00 【I can screw the bottle cap when I am born again】
ladder Nim game
Now? , There is one n Stairs of steps , There are several stones on each step , Among them the first i There are... On the steps ai A stone (i≥1).
Two players take turns , Each operation can take several stones from any step and put them into the next step ( You can't stop taking ).
You can't take the stones on the ground anymore , In the end, those who can't do it are considered failures .
Ask if both of them adopt the optimal strategy , Whether the first is sure to win .
Input format
The first line contains integers n.
The second line contains n It's an integer , Among them the first i An integer represents the th i Number of stones on steps ai.
Output format
If you start first, you will win , The output Yes.
otherwise , Output No.
Data range
1≤n≤105,
1≤ai≤109
sample input :
3
2 1 3
sample output :
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;
}
aggregate Nim game
Given n A pile of stones and a pile made of k A number set of different positive integers S.
Now there are two players taking turns , Each operation can take stones from any pile of stones , The number of stones taken each time must be included in the set S, In the end, those who can't do it are considered failures .
Ask if both of them adopt the optimal strategy , Whether the first is sure to win .
Input format
The first line contains integers k, Represents a set of numbers S Number of numbers in .
The second line contains k It's an integer , Among them the first i An integer represents a set of numbers S No i Number si.
The third line contains integers n.
The fourth line contains n It's an integer , Among them the first i An integer represents the th i The number of stones hi.
Output format
If you start first, you will win , The output Yes.
otherwise , Output No.
Data range
1≤n,k≤100,
1≤si,hi≤10000
sample input :
2
2 5
3
2 4 7
sample output :
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;
}
Split -Nim game
Given n Rubble , Two players take turns , Each operation can remove a pile of stones , Then put in two smaller piles of stones ( The new heap size can be 0, And the total number of stones in the two new piles can be greater than the number of stones taken away ), In the end, those who can't do it are considered failures .
Ask if both of them adopt the optimal strategy , Whether the first is sure to win .
Input format
The first line contains integers n.
The second line contains n It's an integer , Among them the first i An integer represents the th i The number of stones ai.
Output format
If you start first, you will win , The output Yes.
otherwise , Output No.
Data range
1≤n,ai≤100
sample input :
2
2 3
sample output :
Yes
666
Compared to the set -Nim, Each pile here can be changed into two piles of any size no larger than the original pile
namely a[i]a[i] Can be broken down into (b[i],b[j])(b[i],b[j]), To avoid repetition b[i]>=b[j]b[i]>=b[j], namely :a[i]>=b[i]>=b[j]a[i]>=b[i]>=b[j]
It is equivalent to splitting one situation into two situations , from SG Function theory , Multiple independent situations SGSG value , Equal to these situations SGSG The XOR and of values .
So the state that needs to be stored is sg(b[i])sg(b[i])^sg(b[j])sg(b[j])( And assemble -Nim The only difference )
#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年劳务员-通用基础(劳务员)上岗证题目及答案
- Pagoda access changed from IP to domain name
- Full stack development practice | integrated development of SSM framework
- Vivo large scale kubernetes cluster automation operation and maintenance practice
- Finally, the monthly income is 20000!!
- Modification of string class object
- 2021CCPC网络赛题解加总结
- Easyclick run code snippet out null
- 容斥原理(能被整除的数)
- Go zero microservice Practice Series (III. API definition and table structure design)
猜你喜欢
Introduction to binary tree
为发泄对上司不满,百度95后程序员删库被判9个月
flutter简单优秀的开源dialog使用free_dialog
Talk about MySQL indexing mechanism
Folder data synchronization tool sync folders Pro
Install Kubernetes 1.24
Solution to qt5.12 unable to input Chinese (unable to switch Chinese input method) in deepin system
Matplotlib learning notes
Brief introduction to memory structure of virtual machine
On the exploitation of a horizontal ultra vires vulnerability
随机推荐
2022 tailings recurrent training question bank and simulated examination
Ubuntu安装mysql压缩包备查
区间修改乘和加(理解懒标记的好例题)
音视频技术开发周刊 | 249
Acwing game 55
Similarities and differences between decoration mode and agency mode
ACP | 东北地理所在气象-空气质量双向耦合模式研究中取得进展
C# 文件打包下载
Web3 系统构建:去中心化的原则、模型和方法(上)
文件夹数据同步工具Sync Folders Pro
Navicat connection MySQL in Pagoda
Use of servers
About instruction set bits and instruction architecture bits
Software testing often asks, do you really build a testing environment?
Talk about MySQL indexing mechanism
Idea remote debugging jar submitted by spark submit
欧拉函数和线性筛求欧拉函数
Go 要加个箭头语法,这下更像 PHP 了!
COM的模式变化引起的IPdu Handling【接收截止日期监控】
元宇宙土地:是什么让数字房地产变得有价值