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

// abc161_f
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()

#define INF ((1 << 29) - 1)
#define MOD (1000000007)

#define print2D(h, w, arr)                                                     \\
  REP(i, h) {                                                                  \\
    REP(j, w) cout << arr[i][j] << " ";                                        \\
    cout << endl;                                                              \\
  }
#define print_line(vec, n)                                                     \\
  {                                                                            \\
    for (int i = 0; i < (n - 1); i++)                                          \\
      cout << (vec)[i] << " ";                                                 \\
    cout << (vec)[(n)-1] << endl;                                              \\
  }
template <class T> void print(const T &x) { cout << x << endl; }
template <class T, class... A> void print(const T &first, const A &...rest) {
  cout << first << " ";
  print(rest...);
}
struct PreMain {
  PreMain() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} premain;

vector<ll> get_divisors(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (i * i == n) {
      res.emplace_back(i);
    }
    if (n % i == 0) {
      res.emplace_back(i);
      res.emplace_back(n / i);
    }
  }
  sort(ALL(res));
  return res;
}

map<ll, int> factorize(ll n) {
  ll tmp = n;
  map<ll, int> res;
  for (ll a = 2; a * a <= n; a++) {
    int cnt = 0;
    while (tmp % a == 0) {
      cnt++;
      tmp /= a;
    }
    if (cnt > 0)
      res[a] = cnt;
  }
  if (tmp > 1)
    res[tmp] = 1;
  return res;
}

int main() {
#ifdef LOCAL
  ifstream in("../arg.txt");
  cin.rdbuf(in.rdbuf());
#endif

  ll N;
  cin >> N;
  // (aK+1) x K^b

  vector<ll> divisors = get_divisors(N);
  set<ll> st;
  for (auto div : divisors) {
    if (div == 1)
      continue;

    ll k = div;
    ll k_b = 1;
    while (N % (k_b * k) == 0) {
      k_b *= k;
      if ((N / k_b - 1) % k == 0) {
        st.insert(k);
      }

      if (k_b >= (ll)numeric_limits<int>::max() &&
          k >= (ll)numeric_limits<int>::max())
        break; // オーバーフローしちゃう
    }
  }

  vector<ll> divisors2 = get_divisors(N - 1);
  for (auto div : divisors2) {
    if (div == 1)
      continue;
    st.insert(div);
  }

  //    print(divisors);
  //    print(st);

  print(st.size());

  //
  //    int cnt = 0;
  //    REPN(k, 1, N+1){
  //        ll n = N;
  //        while (n % k == 0 && k != 1) n /= k;
  //        if (n % k == 1){
  //            print(k);
  //            cnt++;
  //        }
  //    }
  //    print(cnt);

  return 0;
}


Error: 0

Fixed submission:

// abc161_f
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()

#define INF ((1 << 29) - 1)
#define MOD (1000000007)

#define print2D(h, w, arr)                                                     \\
  REP(i, h) {                                                                  \\
    REP(j, w) cout << arr[i][j] << " ";                                        \\
    cout << endl;                                                              \\
  }
#define print_line(vec, n)                                                     \\
  {                                                                            \\
    for (int i = 0; i < (n - 1); i++)                                          \\
      cout << (vec)[i] << " ";                                                 \\
    cout << (vec)[(n)-1] << endl;                                              \\
  }
template <class T> void print(const T &x) { cout << x << endl; }
template <class T, class... A> void print(const T &first, const A &...rest) {
  cout << first << " ";
  print(rest...);
}
struct PreMain {
  PreMain() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} premain;

vector<ll> get_divisors(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (i * i == n) {
      res.emplace_back(i);
    }
    if (n % i == 0) {
      res.emplace_back(i);
      res.emplace_back(n / i);
    }
  }
  sort(ALL(res));
  return res;
}

map<ll, int> factorize(ll n) {
  ll tmp = n;
  map<ll, int> res;
  for (ll a = 2; a * a <= n; a++) {
    int cnt = 0;
    while (tmp % a == 0) {
      cnt++;
      tmp /= a;
    }
    if (cnt > 0)
      res[a] = cnt;
  }
  if (tmp > 1)
    res[tmp] = 1;
  return res;
}

int main() {
#ifdef LOCAL
  ifstream in("../arg.txt");
  cin.rdbuf(in.rdbuf());
#endif

  ll N;
  cin >> N;
  // (aK+1) x K^b

  vector<ll> divisors = get_divisors(N);
  set<ll> st;
  for (auto div : divisors) {
    if (div == 1)
      continue;

    ll k = div;
    ll k_b = 1;
    while (N % (k_b * k) == 0) {
      if (k_b >= (ll)numeric_limits<int>::max() &&
          k >= (ll)numeric_limits<int>::max())
        break; // オーバーフローしちゃう

      k_b *= k;
      if ((N / k_b - 1) % k == 0) {
        st.insert(k);
      }
    }
  }

  vector<ll> divisors2 = get_divisors(N - 1);
  for (auto div : divisors2) {
    if (div == 1)
      continue;
    st.insert(div);
  }

  //    print(divisors);
  //    print(st);

  print(st.size());

  //
  //    int cnt = 0;
  //    REPN(k, 1, N+1){
  //        ll n = N;
  //        while (n % k == 0 && k != 1) n /= k;
  //        if (n % k == 1){
  //            print(k);
  //            cnt++;
  //        }
  //    }
  //    print(cnt);

  return 0;
}