Problem p02541 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const int SIZE = 51234567;
vector<int> primes;
bool isPrime[SIZE];

void make_primes() {
  if (primes.size() > 0)
    return;
  memset(isPrime, true, sizeof(isPrime));
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i < SIZE; i++)
    if (isPrime[i])
      for (int j = i * i; j < SIZE; j += i)
        isPrime[j] = false;
  REP(i, SIZE) if (isPrime[i]) primes.push_back(i);
}

map<ll, ll> factorize(ll N) {
  map<ll, ll> count;
  for (int i = 0; primes[i] * primes[i] <= N; i++) {
    while (N % primes[i] == 0) {
      count[primes[i]]++;
      N /= primes[i];
    }
  }
  if (N != 1)
    count[N]++;
  return count;
}

vector<int> divisors(int n) {
  vector<int> ds;
  for (int d = 1; d * d <= n; d++) {
    if (n % d == 0) {
      ds.push_back(d);
      if (d * d != n)
        ds.push_back(n / d);
    }
  }
  sort(ds.begin(), ds.end());
  return ds;
}

ll ext_gcd(ll p, ll q, ll &x, ll &y) { // get px+qy=gcd(p,q)
  if (q == 0)
    return x = 1, y = 0, p;
  ll g = ext_gcd(q, p % q, y, x);
  y -= p / q * x;
  return g;
}

bool check(ll k, ll N) {
  auto count = factorize(N);
  count[2]++;

  auto count_k = factorize(k);
  auto count_k1 = factorize(k + 1);

  for (auto it : count) {
    ll p = it.first;
    if (count[p] > count_k[p] + count_k1[p])
      return false;
  }
  return true;
}

int main2() {
  ll N = nextLong();

  auto count = factorize(N);

  // cout << "N=" << N << endl;
  // pv(ALL(count));

  if (count.size() == 1) {
    ll p = count.begin()->first;
    ll m = count.begin()->second;
    if (p == 2) {
      cout << (1LL << (m + 1)) - 1 << endl;
      return 0;
    }
    cout << N - 1 << endl;
    return 0;
  }

  vector<ll> cand;

  vector<ll> p;
  vector<ll> m;
  for (auto it : count) {
    p.push_back(it.first);
    m.push_back(it.second);
  }
  const int M = m.size();

  REP(st, 1 << M) {
    ll s = 1, t = 1;
    REP(i, M)
    if (st >> i & 1) {
      REP(_, m[i]) s *= p[i];
    } else {
      REP(_, m[i]) t *= p[i];
    }
    ll x, y;
    ext_gcd(s, -t, x, y);
    if (t * y <= 2 * N)
      cand.push_back(t * y);
    if (s * x <= 2 * N)
      cand.push_back(s * x);
    // cout << s*x << " " << t * y << endl;
    // cout << x << " " << y << endl;
  }

  sort(ALL(cand));
  // pv(ALL(cand));
  for (ll k : cand)
    if (k > 0) {
      if (check(k, N)) {
        cout << k << endl;
        return 0;
      }
    }
  // cout << "NO SOLUTION" << endl;

  return 0;
}

ll naive(ll N) {
  for (ll k = 1;; k++)
    if ((k * (k + 1) / 2) % N == 0)
      return k;
}

int main() {

  make_primes();

  // for (int N = 1; N <= 100; N++) if (N % 2 == 1) {
  //   cout << "N=" << N << " ";
  //   auto count = factorize(N);
  //   cout << " K=" << naive(N) << " ";
  //   pv(ALL(count));
  // }
  // return 0;

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const int SIZE = 51234567;
vector<ll> primes;
bool isPrime[SIZE];

void make_primes() {
  if (primes.size() > 0)
    return;
  memset(isPrime, true, sizeof(isPrime));
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i < SIZE; i++)
    if (isPrime[i])
      for (int j = i * i; j < SIZE; j += i)
        isPrime[j] = false;
  REP(i, SIZE) if (isPrime[i]) primes.push_back(i);
}

map<ll, ll> factorize(ll N) {
  map<ll, ll> count;
  for (int i = 0; primes[i] * primes[i] <= N; i++) {
    while (N % primes[i] == 0) {
      count[primes[i]]++;
      N /= primes[i];
    }
  }
  if (N != 1)
    count[N]++;
  return count;
}

vector<int> divisors(int n) {
  vector<int> ds;
  for (int d = 1; d * d <= n; d++) {
    if (n % d == 0) {
      ds.push_back(d);
      if (d * d != n)
        ds.push_back(n / d);
    }
  }
  sort(ds.begin(), ds.end());
  return ds;
}

ll ext_gcd(ll p, ll q, ll &x, ll &y) { // get px+qy=gcd(p,q)
  if (q == 0)
    return x = 1, y = 0, p;
  ll g = ext_gcd(q, p % q, y, x);
  y -= p / q * x;
  return g;
}

bool check(ll k, ll N) {
  auto count = factorize(N);
  count[2]++;

  auto count_k = factorize(k);
  auto count_k1 = factorize(k + 1);

  for (auto it : count) {
    ll p = it.first;
    if (count[p] > count_k[p] + count_k1[p])
      return false;
  }
  return true;
}

int main2() {
  ll N = nextLong();

  auto count = factorize(N);

  // cout << "N=" << N << endl;
  // pv(ALL(count));

  if (count.size() == 1) {
    ll p = count.begin()->first;
    ll m = count.begin()->second;
    if (p == 2) {
      cout << (1LL << (m + 1)) - 1 << endl;
      return 0;
    }
    cout << N - 1 << endl;
    return 0;
  }

  vector<ll> cand;

  vector<ll> p;
  vector<ll> m;
  for (auto it : count) {
    p.push_back(it.first);
    m.push_back(it.second);
  }
  const int M = m.size();

  REP(st, 1 << M) {
    ll s = 1, t = 1;
    REP(i, M)
    if (st >> i & 1) {
      REP(_, m[i]) s *= p[i];
    } else {
      REP(_, m[i]) t *= p[i];
    }
    ll x, y;
    ext_gcd(s, -t, x, y);
    if (t * y <= 2 * N)
      cand.push_back(t * y);
    if (s * x <= 2 * N)
      cand.push_back(s * x);
    // cout << s*x << " " << t * y << endl;
    // cout << x << " " << y << endl;
  }

  sort(ALL(cand));
  // pv(ALL(cand));
  for (ll k : cand)
    if (k > 0) {
      if (check(k, N)) {
        cout << k << endl;
        return 0;
      }
    }
  // cout << "NO SOLUTION" << endl;

  return 0;
}

ll naive(ll N) {
  for (ll k = 1;; k++)
    if ((k * (k + 1) / 2) % N == 0)
      return k;
}

int main() {

  make_primes();

  // for (int N = 1; N <= 100; N++) if (N % 2 == 1) {
  //   cout << "N=" << N << " ";
  //   auto count = factorize(N);
  //   cout << " K=" << naive(N) << " ";
  //   pv(ALL(count));
  // }
  // return 0;

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}