Problem p02541 (C++) — buggy submission, verdict: Runtime Error

#line 1 "main.cpp"

/**
 * @title Template
 */

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/chmin_chmax.cpp"

template <class T, class U> constexpr bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> constexpr bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

/**
 * @title Chmin/Chmax
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

class range {
public:
  class iterator {
  private:
    int64_t M_position;

  public:
    constexpr iterator(int64_t position) noexcept : M_position(position) {}
    constexpr void operator++() noexcept { ++M_position; }
    constexpr bool operator!=(iterator other) const noexcept {
      return M_position != other.M_position;
    }
    constexpr int64_t operator*() const noexcept { return M_position; }
  };

  class reverse_iterator {
  private:
    int64_t M_position;

  public:
    constexpr reverse_iterator(int64_t position) noexcept
        : M_position(position) {}
    constexpr void operator++() noexcept { --M_position; }
    constexpr bool operator!=(reverse_iterator other) const noexcept {
      return M_position != other.M_position;
    }
    constexpr int64_t operator*() const noexcept { return M_position; }
  };

private:
  const iterator M_first, M_last;

public:
  constexpr range(int64_t first, int64_t last) noexcept
      : M_first(first), M_last(std::max(first, last)) {}
  constexpr iterator begin() const noexcept { return M_first; }
  constexpr iterator end() const noexcept { return M_last; }
  constexpr reverse_iterator rbegin() const noexcept {
    return reverse_iterator(*M_last - 1);
  }
  constexpr reverse_iterator rend() const noexcept {
    return reverse_iterator(*M_first - 1);
  }
};

/**
 * @title Range
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/rev.cpp"

#include <iterator>
#include <type_traits>
#line 6 "/Users/kodamankod/Desktop/cpp_programming/Library/other/rev.cpp"

template <class T> class rev_impl {
public:
  using iterator = decltype(std::rbegin(std::declval<T>()));

private:
  const iterator M_begin;
  const iterator M_end;

public:
  constexpr rev_impl(T &&cont) noexcept
      : M_begin(std::rbegin(cont)), M_end(std::rend(cont)) {}
  constexpr iterator begin() const noexcept { return M_begin; }
  constexpr iterator end() const noexcept { return M_end; }
};

template <class T> constexpr decltype(auto) rev(T &&cont) {
  return rev_impl<T>(std::forward<T>(cont));
}

/**
 * @title Reverser
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/fact_prime.cpp"

#include <cstddef>
#line 9 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/fact_prime.cpp"
#include <type_traits>

template <class T, std::enable_if_t<std::is_integral<T>::value> * = nullptr>
std::vector<std::pair<T, size_t>> factorize(T x) {
  assert(x > 0);
  if (x == 1)
    return {};
  std::vector<std::pair<T, size_t>> res;
  for (T i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      res.emplace_back(i, 0);
      while (x % i == 0) {
        res.back().second++;
        x /= i;
      }
    }
  }
  if (x > 1) {
    res.emplace_back(x, 1);
  }
  return res;
}

template <class T, std::enable_if_t<std::is_integral<T>::value> * = nullptr>
std::vector<T> divisors(const T x) {
  std::vector<T> small, big;
  for (T i = 1; i * i <= x; ++i) {
    if (x % i == 0) {
      small.push_back(i);
      if (i * i != x) {
        big.push_back(x / i);
      }
    }
  }
  small.reserve(small.size() + big.size());
  std::copy(big.rbegin(), big.rend(), std::back_inserter(small));
  return small;
}

/**
 * @title Factors/Divisors
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/ext_gcd.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/ext_gcd.cpp"
#include <cstdint>

constexpr std::pair<int64_t, int64_t> ext_gcd(int64_t a, int64_t b) {
  if ((a %= b) == 0)
    return {b, 0};
  int64_t s = b, t = (a < 0 ? a + b : a);
  int64_t m0 = 0, m1 = 1, tmp = 0;
  while (t > 0) {
    const auto u = s / t;
    s -= t * u;
    m0 -= m1 * u;
    tmp = s;
    s = t;
    t = tmp;
    tmp = m0;
    m0 = m1;
    m1 = tmp;
  }
  return {s, (m0 < 0 ? m0 + b / s : m0)};
}

/**
 * @title Extended GCD
 */
#line 19 "main.cpp"

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  i64 N;
  std::cin >> N;
  const auto vec = factorize(2 * N);
  const i32 size = vec.size();
  std::vector<i64> prd(size, 1);
  for (auto i : range(0, size)) {
    const auto [p, e] = vec[i];
    for (auto j : range(0, e)) {
      prd[i] *= p;
    }
  }
  i64 ans = inf64;
  for (auto set : range(0, 1 << size)) {
    i64 a = 1, b = 1;
    for (auto i : range(0, size)) {
      if (set >> i & 1) {
        a *= prd[i];
      } else {
        b *= prd[i];
      }
    }
    if (b != 1) {
      const auto a2 = ((-a) % b + b) % b;
      const auto [x, y] = ext_gcd(a2, b);
      chmin(ans, __int128_t(a) * y);
    } else {
      chmin(ans, a);
    }
  }
  std::cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#line 1 "main.cpp"

/**
 * @title Template
 */

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/chmin_chmax.cpp"

template <class T, class U> constexpr bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> constexpr bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

/**
 * @title Chmin/Chmax
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

class range {
public:
  class iterator {
  private:
    int64_t M_position;

  public:
    constexpr iterator(int64_t position) noexcept : M_position(position) {}
    constexpr void operator++() noexcept { ++M_position; }
    constexpr bool operator!=(iterator other) const noexcept {
      return M_position != other.M_position;
    }
    constexpr int64_t operator*() const noexcept { return M_position; }
  };

  class reverse_iterator {
  private:
    int64_t M_position;

  public:
    constexpr reverse_iterator(int64_t position) noexcept
        : M_position(position) {}
    constexpr void operator++() noexcept { --M_position; }
    constexpr bool operator!=(reverse_iterator other) const noexcept {
      return M_position != other.M_position;
    }
    constexpr int64_t operator*() const noexcept { return M_position; }
  };

private:
  const iterator M_first, M_last;

public:
  constexpr range(int64_t first, int64_t last) noexcept
      : M_first(first), M_last(std::max(first, last)) {}
  constexpr iterator begin() const noexcept { return M_first; }
  constexpr iterator end() const noexcept { return M_last; }
  constexpr reverse_iterator rbegin() const noexcept {
    return reverse_iterator(*M_last - 1);
  }
  constexpr reverse_iterator rend() const noexcept {
    return reverse_iterator(*M_first - 1);
  }
};

/**
 * @title Range
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/rev.cpp"

#include <iterator>
#include <type_traits>
#line 6 "/Users/kodamankod/Desktop/cpp_programming/Library/other/rev.cpp"

template <class T> class rev_impl {
public:
  using iterator = decltype(std::rbegin(std::declval<T>()));

private:
  const iterator M_begin;
  const iterator M_end;

public:
  constexpr rev_impl(T &&cont) noexcept
      : M_begin(std::rbegin(cont)), M_end(std::rend(cont)) {}
  constexpr iterator begin() const noexcept { return M_begin; }
  constexpr iterator end() const noexcept { return M_end; }
};

template <class T> constexpr decltype(auto) rev(T &&cont) {
  return rev_impl<T>(std::forward<T>(cont));
}

/**
 * @title Reverser
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/fact_prime.cpp"

#include <cstddef>
#line 9 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/fact_prime.cpp"
#include <type_traits>

template <class T, std::enable_if_t<std::is_integral<T>::value> * = nullptr>
std::vector<std::pair<T, size_t>> factorize(T x) {
  assert(x > 0);
  if (x == 1)
    return {};
  std::vector<std::pair<T, size_t>> res;
  for (T i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      res.emplace_back(i, 0);
      while (x % i == 0) {
        res.back().second++;
        x /= i;
      }
    }
  }
  if (x > 1) {
    res.emplace_back(x, 1);
  }
  return res;
}

template <class T, std::enable_if_t<std::is_integral<T>::value> * = nullptr>
std::vector<T> divisors(const T x) {
  std::vector<T> small, big;
  for (T i = 1; i * i <= x; ++i) {
    if (x % i == 0) {
      small.push_back(i);
      if (i * i != x) {
        big.push_back(x / i);
      }
    }
  }
  small.reserve(small.size() + big.size());
  std::copy(big.rbegin(), big.rend(), std::back_inserter(small));
  return small;
}

/**
 * @title Factors/Divisors
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/modular.cpp"

#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/ext_gcd.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/ext_gcd.cpp"
#include <cstdint>

constexpr std::pair<int64_t, int64_t> ext_gcd(int64_t a, int64_t b) {
  if ((a %= b) == 0)
    return {b, 0};
  int64_t s = b, t = (a < 0 ? a + b : a);
  int64_t m0 = 0, m1 = 1, tmp = 0;
  while (t > 0) {
    const auto u = s / t;
    s -= t * u;
    m0 -= m1 * u;
    tmp = s;
    s = t;
    t = tmp;
    tmp = m0;
    m0 = m1;
    m1 = tmp;
  }
  return {s, (m0 < 0 ? m0 + b / s : m0)};
}

/**
 * @title Extended GCD
 */
#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/modular.cpp"

#line 8 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/modular.cpp"
#include <type_traits>

template <class Modulus> class modular {
public:
  using value_type = uint32_t;
  using cover_type = uint64_t;

  template <class T> static constexpr value_type normalize(T value_) noexcept {
    if (value_ < 0) {
      value_ = -value_;
      value_ %= Modulus::mod();
      if (value_ == 0)
        return 0;
      return Modulus::mod() - value_;
    }
    return value_ % Modulus::mod();
  }

private:
  value_type value;

  template <bool IsPrime, std::enable_if_t<IsPrime> * = nullptr>
  constexpr modular inverse_helper() const noexcept {
    return power(*this, Modulus::mod() - 2);
  }
  template <bool IsPrime, std::enable_if_t<!IsPrime> * = nullptr>
  constexpr modular inverse_helper() const noexcept {
    const auto tmp = ext_gcd(value, Modulus::mod());
    assert(tmp.first == 1);
    return modular(tmp.second);
  }

public:
  constexpr modular() noexcept : value(0) {}
  template <class T>
  explicit constexpr modular(T value_) noexcept : value(normalize(value_)) {}
  template <class T> explicit constexpr operator T() const noexcept {
    return static_cast<T>(value);
  }

  constexpr value_type get() const noexcept { return value; }
  constexpr value_type &extract() noexcept { return value; }
  constexpr modular operator-() const noexcept {
    return modular(Modulus::mod() - value);
  }
  constexpr modular operator~() const noexcept { return inverse(*this); }

  constexpr modular operator+(const modular &rhs) const noexcept {
    return modular(*this) += rhs;
  }
  constexpr modular &operator+=(const modular &rhs) noexcept {
    if ((value += rhs.value) >= Modulus::mod())
      value -= Modulus::mod();
    return *this;
  }

  constexpr modular operator-(const modular &rhs) const noexcept {
    return modular(*this) -= rhs;
  }
  constexpr modular &operator-=(const modular &rhs) noexcept {
    if ((value += Modulus::mod() - rhs.value) >= Modulus::mod())
      value -= Modulus::mod();
    return *this;
  }

  constexpr modular operator*(const modular &rhs) const noexcept {
    return modular(*this) *= rhs;
  }
  constexpr modular &operator*=(const modular &rhs) noexcept {
    value = (cover_type)value * rhs.value % Modulus::mod();
    return *this;
  }

  constexpr modular operator/(const modular &rhs) const noexcept {
    return modular(*this) /= rhs;
  }
  constexpr modular &operator/=(const modular &rhs) noexcept {
    return (*this) *= inverse(rhs);
  }

  constexpr bool zero() const noexcept { return value == 0; }
  constexpr bool operator==(const modular &rhs) const noexcept {
    return value == rhs.value;
  }
  constexpr bool operator!=(const modular &rhs) const noexcept {
    return value != rhs.value;
  }

  friend std::ostream &operator<<(std::ostream &stream, const modular &rhs) {
    return stream << rhs.value;
  }
  friend constexpr modular inverse(const modular &val) noexcept {
    return val.inverse_helper<Modulus::is_prime>();
  }
  friend constexpr modular power(modular val, cover_type exp) noexcept {
    modular res(1);
    for (; exp > 0; exp >>= 1, val *= val)
      if (exp & 1)
        res *= val;
    return res;
  }
};

template <uint32_t Mod, bool IsPrime = true> struct static_modulus {
  static constexpr uint32_t mod() noexcept { return Mod; }
  static constexpr bool is_prime = IsPrime;
};

template <uint32_t Id = 0, bool IsPrime = false> struct dynamic_modulus {
  static uint32_t &mod() noexcept {
    static uint32_t val = 0;
    return val;
  }
  static constexpr bool is_prime = IsPrime;
};

template <uint32_t Mod> using mint32_t = modular<static_modulus<Mod>>;
using rmint32_t = modular<dynamic_modulus<>>;

/*
 * @title Modint
 */
#line 19 "main.cpp"

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

using m32 = rmint32_t;

int main() {
  i64 N;
  std::cin >> N;
  const auto vec = factorize(2 * N);
  const i32 size = vec.size();
  std::vector<i64> prd(size, 1);
  for (auto i : range(0, size)) {
    const auto [p, e] = vec[i];
    for (auto j : range(0, e)) {
      prd[i] *= p;
    }
  }
  i64 ans = inf64;
  for (auto set : range(0, 1 << size)) {
    i64 a = 1, b = 1;
    for (auto i : range(0, size)) {
      if (set >> i & 1) {
        a *= prd[i];
      } else {
        b *= prd[i];
      }
    }
    if (b != 1) {
      dynamic_modulus<>::mod() = b;
      const auto x = inverse(m32(-a)).get();
      chmin(ans, a * x);
    } else {
      chmin(ans, a);
    }
  }
  std::cout << ans << '\\n';
  return 0;
}