当前位置:网站首页>数学知识:台阶-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;
}
边栏推荐
- 【C语言】指针与数组笔试题详解
- 强化学习 Q-learning 实例详解
- [Arduino experiment 17 L298N motor drive module]
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- [fh-gfsk] fh-gfsk signal analysis and blind demodulation research
- [FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
- tp6快速安装使用MongoDB实现增删改查
- 攻克哈希的基本概念与实现
- leetcode 2097 — 合法重新排列数对
- 不登陆或者登录解决oracle数据库账号被锁定。
猜你喜欢
[Arduino experiment 17 L298N motor drive module]
每日一题之干草堆的移动
Matlab Doppler effect produces vibration signal and processing
JDBC courses
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
[Androd] Gradle 使用技巧之模块依赖替换
Esp32 simple speed message test of ros2 (limit frequency)
MySQL - database query - basic query
Cut point of undirected graph
MySQL基础用法02
随机推荐
tp6快速安装使用MongoDB实现增删改查
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
Embrace the safety concept of platform delivery
Matlab Doppler effect produces vibration signal and processing
数学知识:Nim游戏—博弈论
Soft exam information system project manager_ Real topic over the years_ Wrong question set in the second half of 2019_ Morning comprehensive knowledge question - Senior Information System Project Man
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
leetcode:701. Insertion in binary search tree [BST insertion]
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
How wide does the dual inline for bread board need?
leetcode 2097 — 合法重新排列数对
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
MySQL --- 数据库查询 - 基本查询
Leetcode 2097 - Legal rearrangement of pairs
攻克哈希的基本概念与实现
Mongodb common commands of mongodb series
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
MySQL foundation 06 DDL
Basis of information entropy