当前位置:网站首页>数学知识:台阶-Nim游戏—博弈论
数学知识:台阶-Nim游戏—博弈论
2022-07-03 01:03:00 【奋斗吧!骚年!】
题目:AcWing 892. 台阶-Nim游戏
现在,有一个 n 级台阶的楼梯,每级台阶上都有若干个石子,其中第 i 级台阶上有 ai 个石子(i≥1)。
两位玩家轮流操作,每次操作可以从任意一级台阶上拿若干个石子放到下一级台阶中(不能不拿)。
已经拿到地面上的石子不能再拿,最后无法进行操作的人视为失败。
问如果两人都采用最优策略,先手是否必胜。
输入格式
第一行包含整数 n。
第二行包含 n 个整数,其中第 i 个整数表示第 i 级台阶上的石子数 ai。
输出格式
如果先手方必胜,则输出 Yes。
否则,输出 No。
数据范围
1≤n≤105,
1≤ai≤109
输入样例:
3
2 1 3
输出样例:
Yes
题目分析:
这一题与上一题做法差不多
a1^a3^a5…^an!=0则先手必赢
先手必赢策略:
如果拿偶数台阶,则将拿出来的放在下一台阶,则会保持奇数台阶保持不变。
如果拿奇数台阶,则异或不为0,则操作让其异或变为0。
反之,如果异或为0,则后手赢。
#include <iostream>
using namespace std;
int main()
{
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;
}
边栏推荐
- Database SQL language 01 where condition
- 按键精灵打怪学习-自动寻路回打怪点
- matlab 多普勒效应产生振动信号和处理
- Basic remote connection tool xshell
- Database SQL language 02 connection query
- 2022 cable crane driver examination registration and cable crane driver certificate examination
- MySQL basic usage 02
- What operations need attention in the spot gold investment market?
- 【FPGA教程案例5】基于vivado核的ROM设计与实现
- tail -f 、tail -F、tailf的区别
猜你喜欢
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
Basis of information entropy
[Arduino experiment 17 L298N motor drive module]
Embrace the safety concept of platform delivery
异步、郵件、定時三大任務
Dotconnect for PostgreSQL data provider
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
[androd] module dependency replacement of gradle's usage skills
Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm
Arduino dy-sv17f automatic voice broadcast
随机推荐
Arduino dy-sv17f automatic voice broadcast
dotConnect for PostgreSQL数据提供程序
kivy教程之在 Kivy App 中使用 matplotlib 的示例
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
Detailed explanation of Q-learning examples of reinforcement learning
Excel calculates the difference between time and date and converts it into minutes
攻克哈希的基本概念与实现
leetcode 6103 — 从树中删除边的最小分数
LDC Build Shared Library
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
Test shift right: Elk practice of online quality monitoring
MySQL foundation 05 DML language
Kivy tutorial how to create drop-down lists in Kivy
Mongodb common commands of mongodb series
一比特苦逼程序員的找工作經曆
Daily topic: movement of haystack
Specified interval inversion in the linked list
【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
Dotconnect for PostgreSQL data provider
uniapp组件-uni-notice-bar通告栏