cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: ニム (nim)
(include/emthrm/game/nim.hpp)

$N$ 個の山のいずれかから一つ以上を二人が交互に取り、操作できなくなった方を負けとするゲームである。

時間計算量

$O(N)$

仕様

名前 戻り値
template <typename T>
bool nim(const std::vector<T>& a);
盤面が $A$ のときのニムで先手が勝利するか。

参考文献

TODO

Submissons

https://yukicoder.me/submissions/701261

Required by

Verified with

Code

#ifndef EMTHRM_GAME_NIM_HPP_
#define EMTHRM_GAME_NIM_HPP_

#include <vector>

namespace emthrm {

template <typename T>
bool nim(const std::vector<T>& a) {
  long long x = 0;
  for (const T e : a) x ^= e;
  return x != 0;
}

}  // namespace emthrm

#endif  // EMTHRM_GAME_NIM_HPP_
#line 1 "include/emthrm/game/nim.hpp"



#include <vector>

namespace emthrm {

template <typename T>
bool nim(const std::vector<T>& a) {
  long long x = 0;
  for (const T e : a) x ^= e;
  return x != 0;
}

}  // namespace emthrm
Back to top page