当前位置:网站首页>数学知识: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;
}
边栏推荐
- [system analyst's road] Chapter V double disk software engineering (development model development method)
- 异步、邮件、定时三大任务
- Key wizard play strange learning - front desk and Intranet send background verification code
- [C language] branch and loop statements (Part 1)
- Basis of information entropy
- What are the trading forms of spot gold and what are the profitable advantages?
- 【FPGA教程案例6】基于vivado核的双口RAM设计与实现
- The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
- Thinkphp+redis realizes simple lottery
- 按键精灵打怪学习-多线程后台坐标识别
猜你喜欢

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-9

攻克哈希的基本概念与实现

Leetcode 6103 - minimum fraction to delete an edge from the tree

Draw love with go+ to express love to her beloved

Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem

【FH-GFSK】FH-GFSK信号分析与盲解调研究

一位苦逼程序员的找工作经历
![[untitled]](/img/fd/f6b90536f10325a6fdeb68dc49c72d.png)
[untitled]

异步、郵件、定時三大任務

Explain the basic concepts and five attributes of RDD in detail
随机推荐
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
Esp32 simple speed message test of ros2 (limit frequency)
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
基本远程连接工具Xshell
Daily topic: movement of haystack
MySQL foundation 05 DML language
dotConnect for PostgreSQL数据提供程序
Explain the basic concepts and five attributes of RDD in detail
18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
Leetcode 2097 - Legal rearrangement of pairs
[day 29] given an integer, please find its factor number
MySQL foundation 06 DDL
MongoDB系列之MongoDB常用命令
对非ts/js文件模块进行类型扩充
tp6快速安装使用MongoDB实现增删改查
Tp6 fast installation uses mongodb to add, delete, modify and check
The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)
MySQL foundation 04 MySQL architecture