算法基础之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");
  }
相关推荐
郝学胜-神的一滴13 小时前
Linux高性能网络编程基石:epoll核心与文件描述符限制全解
linux·服务器·网络·c++·后端
司马万13 小时前
RUST基础1----数据类型
开发语言·算法·rust
卷福同学13 小时前
Claude Code源码泄露:8大隐藏功能曝光
人工智能·后端·算法
房开民21 小时前
c++总结
java·开发语言·c++
好大哥呀1 天前
C++ 多态
java·jvm·c++
阿豪学编程1 天前
LeetCode724.:寻找数组的中心下标
算法·leetcode
墨韵流芳1 天前
CCF-CSP第41次认证第三题——进程通信
c++·人工智能·算法·机器学习·csp·ccf
hz_zhangrl1 天前
CCF-GESP 等级考试 2026年3月认证C++五级真题解析
c++·青少年编程·程序设计·gesp·c++五级·gesp2026年3月·gesp c++五级
Σίσυφος19001 天前
C++ 多肽经典面试题
开发语言·c++·面试
csdn_aspnet1 天前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#