算法基础之Nim游戏

Nim游戏

  • 核心思想:博弈论

    • 结论:将所有堆的石子数全部异或起来 得到的结果
    • 若为1 则先手必胜 若为0 则先手必败
cpp 复制代码
  #include <iostream>
  #include <cstring>
  #include <algorithm>
  
  using namespace std;
  const int N = 100010;
  
  int a[N];
  int n;
  
  int main()
  {
      cin>>n;
      int x;
      cin>>x;
      int res = x;
      for(int i=1;i<n;i++) cin>>x, res ^= x;
      
      if(res) puts("Yes");
      else puts("No");
  }
相关推荐
ZC跨境爬虫13 分钟前
LeetCode 219. 存在重复元素 II(滑动窗口 + 哈希表详解)
算法·leetcode·散列表
zh路西法17 分钟前
【玩转VLA具身智能机械臂】(三):视觉避障——从 RGB-D 点云到 MoveIt octomap 的完整落地
c++·ros2·gazebo·moveit2·ompl·octomap
吞下星星的少年·-·23 分钟前
The 2025 ICPC Asia East Continent Online Contest (I) A题(模拟+贪心)
算法·贪心·模拟
小程序设计43 分钟前
【机械设计】磁粉检测机器人的设计与验证
算法·机器人
Navigator_Z1 小时前
LeetCode //C - 1220. Count Vowels Permutation
c语言·算法·leetcode
吃好睡好便好1 小时前
判断函数的使用
学习·算法·matlab·生活·判断函数
2601_956121971 小时前
线性DP(入门)
c++·算法·动态规划
彷徨而立1 小时前
【C/C++】多线程读写普通 int 变量的一些问题
java·c语言·c++
feilieren1 小时前
leetcode - 389. 找不同
算法·leetcode
挽星安1 小时前
2026/8/29
数据结构·算法