当前位置:网站首页>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;
}
边栏推荐
- 硬件工程师薪资虚高,你认可吗?
- Electrolytic capacitor, tantalum capacitor, ordinary capacitor
- SSM integration preliminary details
- Simple query cost estimation [Gauss is not a mathematician this time]
- Pagoda add a website: PHP project
- Environ. Sci. Technol.(IF=9.028) | 城市绿化对大气环境的影响
- EasyClick 运行代码片段出Null
- Ubuntu installs MySQL compressed package for future reference
- Redis related
- 求组合数四种方法
猜你喜欢

View the default MySQL password in the pagoda

数据库学习笔记(第十六章)

Idea remote debugging jar submitted by spark submit

Multithreading starts from the lockless queue of UE4 (thread safe)

状态压缩DP例题(旅行商问题和填矩形问题)

China SaaS industry panorama

Pagoda access changed from IP to domain name

Introduction to binary tree

【20220526】UE5.0.2 release d11782b

服务器的使用
随机推荐
Do you agree that the salary of hardware engineers is falsely high?
EasyClick 运行代码片段出Null
Use of servers
Finally, the monthly income is 20000!!
Similarities and differences between decoration mode and agency mode
Initial installation and use of redis [play with Huawei cloud]
状态压缩DP例题(旅行商问题和填矩形问题)
乘法逆元作用
vivo大规模 Kubernetes 集群自动化运维实践
2021CCPC网络赛榜单
Matplotlib learning notes
of_ find_ compatible_ Node find all nodes
ue5 小知识点 random point in Bounding Boxf From Stream
Brief introduction to memory structure of virtual machine
ADG standby mrp0 status wait_ FOR_ GAP
统计特殊子序列数目(0,1,2)DP
DNS protocol analysis
d编译时生成唯一标识
《自然-通讯》| 用机器学习和时间序列数据为气候变化下的武装冲突风险建模
Codeforces Round #798 (Div. 2)ABCD