当前位置:网站首页>数学知识: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;
}
边栏推荐
- 不登陆或者登录解决oracle数据库账号被锁定。
- leetcode 2097 — 合法重新排列数对
- Excel if formula determines whether the two columns are the same
- ThinkPHP+Redis实现简单抽奖
- Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
- Draw love with go+ to express love to her beloved
- 机器学习术语
- Swiftui component Encyclopedia: using scenekit and swiftui to build interactive 3D pie charts (tutorial with source code)
- JS inheritance and prototype chain
- excel表格计算时间日期的差值,并转化为分钟数
猜你喜欢
随机推荐
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
信息熵的基础
【无标题】
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
寻找标杆战友 | 百万级实时数据平台,终身免费使用
按键精灵打怪学习-前台和内网发送后台验证码
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
Top ten regular spot trading platforms 2022
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
Assets, vulnerabilities, threats and events of the four elements of safe operation
R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
【第29天】给定一个整数,请你求出它的因子数
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
鏈錶內指定區間反轉
Do not log in or log in to solve the problem that the Oracle database account is locked.
Makefile中wildcard、patsubst、notdir的含义
看疫情之下服装企业如何顺势而为
2022 cable crane driver examination registration and cable crane driver certificate examination



![[untitled]](/img/fd/f6b90536f10325a6fdeb68dc49c72d.png)




