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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (int i = 2; i <= n; ++i) {
      if (!f[i]) {
        primes.emplace_back(i);
        f[i] = i;
      }
      for (int p : primes) {
        int u = p * i;
        if (u > n or p > f[i])
          break;
        f[u] = p;
      }
    }
  }
  // 素数判定
  bool isPrime(int x) { return f[x] == x; }
  // 素数判定(1つ用)(宣言はsqrt(x)のサイズで)
  bool isPrime1(int64_t x) {
    bool m = true;
    for (int z : primes) {
      if (!(x % z)) {
        m = false;
        break;
      }
    }
    return m;
  }
  // 素数列挙
  vector<int> factorList(int x) {
    assert(x <= n);
    vector<int> res;
    while (x != 1) {
      res.emplace_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  // 素因数分解　pair(素数, 素因数の数)
  vector<pair<int, int>> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<int, int>> res(1, pair<int, int>(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        ++res.back().second;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
  // 素数列挙(1つ用)(宣言はsqrt(x)のサイズで)
  vector<int64_t> factorList1(int64_t x) {
    vector<int64_t> vec;
    for (int z : primes) {
      while (!(x % z)) {
        vec.emplace_back(z);
        x /= z;
      }
    }
    if (x != 1)
      vec.emplace_back(x);
    return vec;
  }
  // 素数列挙(1つ用)(宣言はsqrt(x)のサイズで)　pair(素数, 素因数の数)
  vector<pair<int64_t, int>> factor1(int64_t x) {
    vector<int64_t> vec = factorList1(x);
    vector<pair<int64_t, int>> vecc;
    for (vector<int64_t>::iterator itr = vec.begin(); itr != vec.end(); ++itr) {
      if (itr != vec.begin() && *itr == *(itr - 1)) {
        (vecc.end() - 1)->second++;
      } else {
        vecc.emplace_back(pair<int64_t, int>(*itr, 1));
      }
    }
    return vecc;
  }
  // 約数列挙
  vector<int64_t> div1(int64_t x) {
    vector<int64_t> res(1, 1);
    vector<pair<int64_t, int>> fac = factor1(x);
    for (int i = 0; i < fac.size(); ++i) {
      int si = res.size();
      for (int j = 0; j < fac[i].second; ++j) {
        for (int k = 0; k < si; ++k) {
          res.emplace_back(res[k + j * si] * fac[i].first);
        }
      }
    }
    sort(res.begin(), res.end());
    return res;
  }
} pp(50000000);

struct barrett {
  unsigned int _m;
  uint64_t im;
  barrett(unsigned int m) : _m(m), im(uint64_t(-1) / m + 1) {}
  unsigned int umod() const { return _m; }
  unsigned int mul(unsigned int a, unsigned int b) const {
    uint64_t z = a;
    z *= b;
    uint64_t x = uint64_t(((unsigned __int128)(z)*im) >> 64);
    unsigned int v = (unsigned int)(z - x * _m);
    if (_m <= v)
      v += _m;
    return v;
  }
};

constexpr int64_t safe_mod(int64_t x, int64_t m) {
  x %= m;
  if (x < 0)
    x += m;
  return x;
}

constexpr pair<int64_t, int64_t> inv_gcd(int64_t a, int64_t b) {
  a = safe_mod(a, b);
  if (a == 0)
    return {b, 0};
  int64_t s = b, t = a;
  int64_t m0 = 0, m1 = 1;
  while (t) {
    int64_t u = s / t;
    s -= t * u;
    m0 -= m1 * u;
    int64_t tmp = s;
    s = t;
    t = tmp;
    tmp = m0;
    m0 = m1;
    m1 = tmp;
  }
  if (m0 < 0)
    m0 += b / s;
  return {s, m0};
}

int64_t pow_mod(int64_t x, int64_t n, int m) {
  if (m == 1)
    return 0;
  barrett bt((unsigned int)(m));
  unsigned int r = 1, y = (unsigned int)(safe_mod(x, m));
  while (n) {
    if (n & 1)
      r = bt.mul(r, y);
    y = bt.mul(y, y);
    n >>= 1;
  }
  return r;
}

int64_t inv_mod(int64_t x, int64_t m) {
  pair<int64_t, int64_t> z = inv_gcd(x, m);
  assert(z.first == 1);
  return z.second;
}

//(y, z) answer mod z = y, 無理な時は(-1, -1)
pair<int64_t, int64_t> crt(const vector<int64_t> &r, const vector<int64_t> &m) {
  int n = int(r.size());
  int64_t r0 = 0, m0 = 1;
  for (int i = 0; i < n; i++) {
    assert(1 <= m[i]);
    int64_t r1 = safe_mod(r[i], m[i]), m1 = m[i];
    if (m0 < m1) {
      swap(r0, r1);
      swap(m0, m1);
    }
    if (m0 % m1 == 0) {
      if (r0 % m1 != r1)
        return {-1, -1};
      continue;
    }
    int64_t g, im;
    tie(g, im) = inv_gcd(m0, m1);
    int64_t u1 = (m1 / g);
    if ((r1 - r0) % g)
      return {-1, -1};
    int64_t x = (r1 - r0) / g % u1 * im % u1;
    r0 += x * m0;
    m0 *= u1;
    if (r0 < 0)
      r0 += m0;
  }
  return {r0, m0};
}

//[0, n) (a*i+b)/m
int64_t floor_sum(int64_t n, int64_t m, int64_t a, int64_t b) {
  int64_t ans = 0;
  if (a >= m) {
    ans += ((n - 1) * n >> 1) * (a / m);
    a %= m;
  }
  if (b >= m) {
    ans += n * (b / m);
    b %= m;
  }
  int64_t y_max = (a * n + b) / m, x_max = y_max * m - b;
  if (y_max == 0)
    return ans;
  ans += (n - (x_max + a - 1) / a) * y_max;
  ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
  return ans;
}

// head

ll ans = INT64_MAX;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  n <<= 1;
  auto v = pp.factor1(n);
  n >>= 1;
  int u = v.size();
  vector<ll> z(u);
  rep(i, u) {
    z[i] = 1;
    rep(j, v[i].second) z[i] *= v[i].first;
  }
  vector<ll> x(u);
  rep(i, 1 << u) {
    rep(j, u) { x[j] = i >> j & 1; }
    auto y = crt(x, z);
    ll h = y.first;
    if ((__int128_t(h) * (h - 1) / 2) < n)
      h += n * 2;
    chmin(ans, h - 1);
  }
  cout << ans << endl;

  // cout << (__int128_t(ans)*(ans+1)/2 % n?"false":"true") << endl;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (int i = 2; i <= n; ++i) {
      if (!f[i]) {
        primes.emplace_back(i);
        f[i] = i;
      }
      for (int p : primes) {
        int u = p * i;
        if (u > n or p > f[i])
          break;
        f[u] = p;
      }
    }
  }
  // 素数判定
  bool isPrime(int x) { return f[x] == x; }
  // 素数判定(1つ用)(宣言はsqrt(x)のサイズで)
  bool isPrime1(int64_t x) {
    bool m = true;
    for (int z : primes) {
      if (!(x % z)) {
        m = false;
        break;
      }
    }
    return m;
  }
  // 素数列挙
  vector<int> factorList(int x) {
    assert(x <= n);
    vector<int> res;
    while (x != 1) {
      res.emplace_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  // 素因数分解　pair(素数, 素因数の数)
  vector<pair<int, int>> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<int, int>> res(1, pair<int, int>(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        ++res.back().second;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
  // 素数列挙(1つ用)(宣言はsqrt(x)のサイズで)
  vector<int64_t> factorList1(int64_t x) {
    vector<int64_t> vec;
    for (int z : primes) {
      while (!(x % z)) {
        vec.emplace_back(z);
        x /= z;
      }
    }
    if (x != 1)
      vec.emplace_back(x);
    return vec;
  }
  // 素数列挙(1つ用)(宣言はsqrt(x)のサイズで)　pair(素数, 素因数の数)
  vector<pair<int64_t, int>> factor1(int64_t x) {
    vector<int64_t> vec = factorList1(x);
    vector<pair<int64_t, int>> vecc;
    for (vector<int64_t>::iterator itr = vec.begin(); itr != vec.end(); ++itr) {
      if (itr != vec.begin() && *itr == *(itr - 1)) {
        (vecc.end() - 1)->second++;
      } else {
        vecc.emplace_back(pair<int64_t, int>(*itr, 1));
      }
    }
    return vecc;
  }
  // 約数列挙
  vector<int64_t> div1(int64_t x) {
    vector<int64_t> res(1, 1);
    vector<pair<int64_t, int>> fac = factor1(x);
    for (int i = 0; i < fac.size(); ++i) {
      int si = res.size();
      for (int j = 0; j < fac[i].second; ++j) {
        for (int k = 0; k < si; ++k) {
          res.emplace_back(res[k + j * si] * fac[i].first);
        }
      }
    }
    sort(res.begin(), res.end());
    return res;
  }
} pp(35000000);

struct barrett {
  unsigned int _m;
  uint64_t im;
  barrett(unsigned int m) : _m(m), im(uint64_t(-1) / m + 1) {}
  unsigned int umod() const { return _m; }
  unsigned int mul(unsigned int a, unsigned int b) const {
    uint64_t z = a;
    z *= b;
    uint64_t x = uint64_t(((unsigned __int128)(z)*im) >> 64);
    unsigned int v = (unsigned int)(z - x * _m);
    if (_m <= v)
      v += _m;
    return v;
  }
};

constexpr int64_t safe_mod(int64_t x, int64_t m) {
  x %= m;
  if (x < 0)
    x += m;
  return x;
}

constexpr pair<int64_t, int64_t> inv_gcd(int64_t a, int64_t b) {
  a = safe_mod(a, b);
  if (a == 0)
    return {b, 0};
  int64_t s = b, t = a;
  int64_t m0 = 0, m1 = 1;
  while (t) {
    int64_t u = s / t;
    s -= t * u;
    m0 -= m1 * u;
    int64_t tmp = s;
    s = t;
    t = tmp;
    tmp = m0;
    m0 = m1;
    m1 = tmp;
  }
  if (m0 < 0)
    m0 += b / s;
  return {s, m0};
}

int64_t pow_mod(int64_t x, int64_t n, int m) {
  if (m == 1)
    return 0;
  barrett bt((unsigned int)(m));
  unsigned int r = 1, y = (unsigned int)(safe_mod(x, m));
  while (n) {
    if (n & 1)
      r = bt.mul(r, y);
    y = bt.mul(y, y);
    n >>= 1;
  }
  return r;
}

int64_t inv_mod(int64_t x, int64_t m) {
  pair<int64_t, int64_t> z = inv_gcd(x, m);
  assert(z.first == 1);
  return z.second;
}

//(y, z) answer mod z = y, 無理な時は(-1, -1)
pair<int64_t, int64_t> crt(const vector<int64_t> &r, const vector<int64_t> &m) {
  int n = int(r.size());
  int64_t r0 = 0, m0 = 1;
  for (int i = 0; i < n; i++) {
    assert(1 <= m[i]);
    int64_t r1 = safe_mod(r[i], m[i]), m1 = m[i];
    if (m0 < m1) {
      swap(r0, r1);
      swap(m0, m1);
    }
    if (m0 % m1 == 0) {
      if (r0 % m1 != r1)
        return {-1, -1};
      continue;
    }
    int64_t g, im;
    tie(g, im) = inv_gcd(m0, m1);
    int64_t u1 = (m1 / g);
    if ((r1 - r0) % g)
      return {-1, -1};
    int64_t x = (r1 - r0) / g % u1 * im % u1;
    r0 += x * m0;
    m0 *= u1;
    if (r0 < 0)
      r0 += m0;
  }
  return {r0, m0};
}

//[0, n) (a*i+b)/m
int64_t floor_sum(int64_t n, int64_t m, int64_t a, int64_t b) {
  int64_t ans = 0;
  if (a >= m) {
    ans += ((n - 1) * n >> 1) * (a / m);
    a %= m;
  }
  if (b >= m) {
    ans += n * (b / m);
    b %= m;
  }
  int64_t y_max = (a * n + b) / m, x_max = y_max * m - b;
  if (y_max == 0)
    return ans;
  ans += (n - (x_max + a - 1) / a) * y_max;
  ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
  return ans;
}

// head

ll ans = INT64_MAX;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  auto v = pp.factor1(n);
  int u = v.size();
  vector<ll> z(u);
  if (v[0].first == 2)
    v[0].second++;
  else {
    v.emplace_back(2, 1);
    u++;
  }
  rep(i, u) {
    z[i] = 1;
    rep(j, v[i].second) z[i] *= v[i].first;
  }
  vector<ll> x(u);
  rep(i, 1 << u) {
    rep(j, u) { x[j] = i >> j & 1; }
    auto y = crt(x, z);
    ll h = y.first;
    if ((h * (h + 1) >> 1) < n)
      h += n * 2;
    chmin(ans, h - 1);
  }
  cout << ans << endl;
}