当前位置:网站首页>数学知识: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;
}
边栏推荐
- Top ten regular spot trading platforms 2022
- [C language] detailed explanation of pointer and array written test questions
- 【无标题】
- Database SQL language 01 where condition
- Esp32 simple speed message test of ros2 (limit frequency)
- dotConnect for PostgreSQL数据提供程序
- Excel removes the data after the decimal point and rounds the number
- ROS2之ESP32简单速度消息测试(极限频率)
- [fh-gfsk] fh-gfsk signal analysis and blind demodulation research
- Telephone network problems
猜你喜欢

leetcode 2097 — 合法重新排列数对

Database SQL language 02 connection query

matlab 多普勒效应产生振动信号和处理

Database SQL language 01 where condition

Androd Gradle 对其使用模块依赖的替换

MySQL foundation 04 MySQL architecture

Basic concept and implementation of overcoming hash

excel表格计算时间日期的差值,并转化为分钟数
![1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]](/img/bf/ab6838e34a3074130eac0a9992e77c.png)
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]

MySQL foundation 05 DML language
随机推荐
比较版本号
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core
MySQL - database query - basic query
用Go+绘制爱心给心爱的她表白
按键精灵打怪学习-前台和内网发送后台验证码
JS inheritance and prototype chain
Look at how clothing enterprises take advantage of the epidemic
Usage of using clause in kingbases alter table
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
电话网络问题
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
matlab 多普勒效应产生振动信号和处理
如今少年已归来,人间烟火气最抚凡人心 复工了~
2022 cable crane driver examination registration and cable crane driver certificate examination
[shutter] animation animation (shutter animation type | the core class of shutter animation)
R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
Arduino DY-SV17F自动语音播报
Matlab finds the position of a row or column in the matrix
[自我管理]时间、精力与习惯管理
[C language] detailed explanation of pointer and array written test questions