当前位置:网站首页>数学知识:Nim游戏—博弈论
数学知识:Nim游戏—博弈论
2022-07-03 01:03:00 【奋斗吧!骚年!】
题目:AcWing 891. Nim游戏
给定 n 堆石子,两位玩家轮流操作,每次操作可以从任意一堆石子中拿走任意数量的石子(可以拿完,但不能不拿),最后无法进行操作的人视为失败。
问如果两人都采用最优策略,先手是否必胜。
输入格式
第一行包含整数 n。
第二行包含 n 个数字,其中第 i 个数字表示第 i 堆石子的数量。
输出格式
如果先手方必胜,则输出 Yes。
否则,输出 No。
数据范围
1≤n≤105,
1≤每堆石子数≤109
输入样例:
2
2 3
输出样例:
Yes
题目分析:
该题是博弈论的题目,
必胜状态——>操作完能够让游戏处于先手必败状态
这道题使用异或,
a1,a2,a3,…an表示n堆石子的数量
如果a1^a2^a3,…^an = 0 先手必败
如果a1^a2^a3,…^an!=0 先手必胜
如果异或值不为0,则一定可以使异或值为0
如果异或值为0,则不管怎么拿都不会让异或值为0
因为0^0^0=0
所以综上所述,得出结论
#include <iostream>
using namespace std;
const int N = 100010;
int main()
{
int n,res=0;
cin>>n;
while(n--)
{
int x;
cin>>x;
res ^= x;
}
if(res)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
边栏推荐
- ThinkPHP+Redis实现简单抽奖
- d,ldc構建共享庫
- Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
- Database SQL language 02 connection query
- Excel calculates the difference between time and date and converts it into minutes
- Mongodb common commands of mongodb series
- MySQL basic usage 02
- [androd] module dependency replacement of gradle's usage skills
- MySQL基础用法02
- 对非ts/js文件模块进行类型扩充
猜你喜欢
随机推荐
How wide does the dual inline for bread board need?
The meaning of wildcard, patsubst and notdir in makefile
有向图的强连通分量
excel去除小数点后面的数据,将数字取整
Database SQL language 02 connection query
按键精灵打怪学习-多线程后台坐标识别
18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
Matlab Doppler effect produces vibration signal and processing
Excel if formula determines whether the two columns are the same
Excel removes the data after the decimal point and rounds the number
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
[shutter] animation animation (shutter animation type | the core class of shutter animation)
每日一题之干草堆的移动
链表中的节点每k个一组翻转
【C语言】指针与数组笔试题详解
Merge K sorted linked lists
Thinkphp+redis realizes simple lottery
Niu Ke swipes questions and clocks in