cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:warning: misère Nim
(include/emthrm/game/misere_nim.hpp)

ニムのルールの内、操作できなくなった方を勝ちとするものである。

時間計算量

$O(N)$

仕様

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

参考文献

Depends on

Code

#ifndef EMTHRM_GAME_MISERE_NIM_HPP_
#define EMTHRM_GAME_MISERE_NIM_HPP_

#include <algorithm>
#include <vector>

#include "emthrm/game/nim.hpp"

namespace emthrm {

template <typename T>
bool misere_nim(const std::vector<T>& a) {
  std::vector<T> positive;
  positive.reserve(a.size());
  for (const T e : a) {
    if (e > 0) positive.emplace_back(e);
  }
  if (positive.empty()) [[unlikely]] return true;
  return *std::max_element(positive.begin(), positive.end()) == 1 ?
         positive.size() % 2 == 0 : nim(positive);
}

}  // namespace emthrm

#endif  // EMTHRM_GAME_MISERE_NIM_HPP_
#line 1 "include/emthrm/game/misere_nim.hpp"



#include <algorithm>
#include <vector>

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



#line 5 "include/emthrm/game/nim.hpp"

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


#line 8 "include/emthrm/game/misere_nim.hpp"

namespace emthrm {

template <typename T>
bool misere_nim(const std::vector<T>& a) {
  std::vector<T> positive;
  positive.reserve(a.size());
  for (const T e : a) {
    if (e > 0) positive.emplace_back(e);
  }
  if (positive.empty()) [[unlikely]] return true;
  return *std::max_element(positive.begin(), positive.end()) == 1 ?
         positive.size() % 2 == 0 : nim(positive);
}

}  // namespace emthrm
Back to top page