算法基础之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");
  }
相关推荐
To_OC10 小时前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
金銀銅鐵13 小时前
[Python] 扩展欧几里得算法
python·数学·算法
To_OC16 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
金銀銅鐵21 小时前
借助 Pygame 探索最大公约数的规律
python·数学·游戏
郝学胜_神的一滴21 小时前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
To_OC1 天前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode
05Kevin2 天前
lk每日冒险题--数据结构6.27
算法
To_OC2 天前
从一次栈溢出报错说起,我把递归彻底扒明白了
javascript·算法·程序员