当前位置:网站首页>数学知识:台阶-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;
}
边栏推荐
- Assets, vulnerabilities, threats and events of the four elements of safe operation
- 不登陆或者登录解决oracle数据库账号被锁定。
- Create your first Kivy program Hello word (tutorial includes source code)
- Kivy tutorial - example of using Matplotlib in Kivy app
- Test shift right: Elk practice of online quality monitoring
- [self management] time, energy and habit management
- Kivy tutorial how to create drop-down lists in Kivy
- Thinkphp+redis realizes simple lottery
- Swiftui component Encyclopedia: using scenekit and swiftui to build interactive 3D pie charts (tutorial with source code)
- Look at how clothing enterprises take advantage of the epidemic
猜你喜欢

【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理

Excel calculates the difference between time and date and converts it into minutes

MySQL basics 03 introduction to MySQL types

有向图的强连通分量

力扣 204. 计数质数

Work experience of a hard pressed programmer

MySQL --- 数据库查询 - 条件查询

Embrace the safety concept of platform delivery

JDBC courses

一位苦逼程序员的找工作经历
随机推荐
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
[C language] detailed explanation of pointer and array written test questions
ROS2之ESP32简单速度消息测试(极限频率)
Cut point of undirected graph
【面试题】1369- 什么时候不能使用箭头函数?
无向图的割点
MySQL基础用法02
Embrace the safety concept of platform delivery
Kivy教程大全之如何在 Kivy 中创建下拉列表
Daily topic: movement of haystack
R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
Detailed explanation of Q-learning examples of reinforcement learning
攻克哈希的基本概念与实现
excel去除小数点后面的数据,将数字取整
按鍵精靈打怪學習-多線程後臺坐標識別
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
Button wizard play strange learning - automatic return to the city route judgment
数学知识:Nim游戏—博弈论
kivy教程之在 Kivy App 中使用 matplotlib 的示例