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

#ifdef DEBUG_IS_VALID
#define DEB 1
#define _LIBCPP_DEBUG 0
#else
#define DEB 0
#define NDEBUG
#endif

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

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (long long i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, x, n) for (long long i = x; i >= n; i--)
#define rrep(i, n) RREP(i, n, 0)
#define pb push_back
#define fi first
#define se second
#pragma GCC optimize("-O3")

using namespace std;

#define DUMPOUT cout
#define dump(...)                                                              \\
  if (DEB)                                                                     \\
    DUMPOUT << "  " << #__VA_ARGS__ << " :[" << __LINE__ << ":"                \\
            << __FUNCTION__ << "]" << endl                                     \\
            << "    ";                                                         \\
  if (DEB)                                                                     \\
  dump_func(__VA_ARGS__)
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  cout << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &st) {
  cout << "{";
  for (auto itr = st.begin(); itr != st.end(); itr++)
    cout << *itr << (next(itr) != st.end() ? ", " : "");
  cout << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> mp) {
  cout << "{";
  for (auto itr = mp.begin(); itr != mp.end(); itr++)
    cout << "(" << (itr->first) << ", " << (itr->second) << ")"
         << (next(itr) != mp.end() ? "," : "");
  cout << "}";
  return os;
}

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) == 0) {
    DUMPOUT << " ";
  } else {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
template <class T> inline bool chmax(T &a, T const &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}
template <class T> inline bool chmin(T &a, T const &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

using Int = __int128;
using ll = __int128;
using P = pair<Int, Int>;
using vi = vector<Int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vvp = vector<vp>;

const Int INF = (1LL << 50);

template <int MOD> struct ModInt {
  static const int Mod = MOD;
  unsigned x;
  ModInt() : x(0) {}
  ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  int get() const { return (int)x; }
  ModInt &operator+=(ModInt that) {
    if ((x += that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(ModInt that) {
    if ((x += MOD - that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator*=(ModInt that) {
    x = (unsigned long long)x * that.x % MOD;
    return *this;
  }
  ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
  ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
  ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  ModInt inverse() const {
    long long a = x, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  bool operator==(ModInt that) const { return x == that.x; }
  bool operator!=(ModInt that) const { return x != that.x; }
  ModInt operator-() const {
    ModInt t;
    t.x = x == 0 ? 0 : Mod - x;
    return t;
  }
};
template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) {
  st << a.get();
  return st;
};
template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
  ModInt<MOD> r = 1;
  while (k) {
    if (k & 1)
      r *= a;
    a *= a;
    k >>= 1;
  }
  return r;
}
template <typename T, int FAC_MAX> struct Comb {
  vector<T> fac, ifac;
  Comb() {
    fac.resize(FAC_MAX, 1);
    ifac.resize(FAC_MAX, 1);
    for (int i = 1; i < FAC_MAX; i++)
      fac[i] = fac[i - 1] * i;
    ifac[FAC_MAX - 1] = T(1) / fac[FAC_MAX - 1];
    for (int i = FAC_MAX - 2; i >= 1; i--)
      ifac[i] = ifac[i + 1] * T(i + 1);
  }
  T aPb(int a, int b) {
    if (b < 0 || a < b)
      return T(0);
    return fac[a] * ifac[a - b];
  }
  T aCb(int a, int b) {
    if (b < 0 || a < b)
      return T(0);
    return fac[a] * ifac[a - b] * ifac[b];
  }
  T nHk(int n, int k) {
    if (n == 0 && k == 0)
      return T(1);
    if (n <= 0 || k < 0)
      return 0;
    return aCb(n + k - 1, k);
  }
}; // nHk = (n+k-1)Ck : n is separator

using mint = ModInt<int(1e9 + 7)>;
using vm = vector<mint>;

std::ostream &operator<<(std::ostream &dest, __int128_t value) {
  std::ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = std::end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = std::end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(std::ios_base::badbit);
    }
  }
  return dest;
}

__int128 parse(string &s) {
  __int128 ret = 0;
  for (int i = 0; i < s.length(); i++)
    if ('0' <= s[i] && s[i] <= '9')
      ret = 10 * ret + s[i] - '0';
  return ret;
}

map<ll, ll> enumpr(ll n) {
  map<ll, ll> V;
  for (ll i = 2; 1LL * i * i <= n; i++)
    while (n % i == 0)
      V[i]++, n /= i;
  if (n > 1)
    V[n]++;
  return V;
}

// 約数の列挙
vector<ll> divisors(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (n / i != i)
        res.push_back(n / i);
    }
  }
  return res;
}

void solve(Int N) {
  auto pr = divisors(N);
  dump(pr);
  set<Int> st;
  for (auto &&k : pr) {
    if (k == 1)
      continue;
    Int kj = 1;
    for (Int j = 1; N % kj == 0; kj *= k) {
      Int rest = N / kj;
      if (rest % k == 1LL && k > 1LL)
        st.insert(k);
    }
  }
  // if kj == 1
  Int ki = N - 1;
  auto div = divisors(ki);
  for (auto &&k : div)
    if (k > 1)
      st.insert(k);
  dump(st);
  cout << st.size() << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  string N;
  cin >> N;
  Int nn = parse(N);
  solve(nn);
  return 0;
}


Error: 0

Fixed submission:

#ifdef DEBUG_IS_VALID
#define DEB 1
#define _LIBCPP_DEBUG 0
#else
#define DEB 0
#define NDEBUG
#endif

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

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (long long i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, x, n) for (long long i = x; i >= n; i--)
#define rrep(i, n) RREP(i, n, 0)
#define pb push_back
#define fi first
#define se second
#pragma GCC optimize("-O3")

using namespace std;

#define DUMPOUT cout
#define dump(...)                                                              \\
  if (DEB)                                                                     \\
    DUMPOUT << "  " << #__VA_ARGS__ << " :[" << __LINE__ << ":"                \\
            << __FUNCTION__ << "]" << endl                                     \\
            << "    ";                                                         \\
  if (DEB)                                                                     \\
  dump_func(__VA_ARGS__)
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  cout << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &st) {
  cout << "{";
  for (auto itr = st.begin(); itr != st.end(); itr++)
    cout << *itr << (next(itr) != st.end() ? ", " : "");
  cout << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> mp) {
  cout << "{";
  for (auto itr = mp.begin(); itr != mp.end(); itr++)
    cout << "(" << (itr->first) << ", " << (itr->second) << ")"
         << (next(itr) != mp.end() ? "," : "");
  cout << "}";
  return os;
}

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) == 0) {
    DUMPOUT << " ";
  } else {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
template <class T> inline bool chmax(T &a, T const &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}
template <class T> inline bool chmin(T &a, T const &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

using Int = long long;
using ll = long long;
using P = pair<Int, Int>;
using vi = vector<Int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vvp = vector<vp>;

const Int INF = (1LL << 50);

template <int MOD> struct ModInt {
  static const int Mod = MOD;
  unsigned x;
  ModInt() : x(0) {}
  ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  int get() const { return (int)x; }
  ModInt &operator+=(ModInt that) {
    if ((x += that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(ModInt that) {
    if ((x += MOD - that.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator*=(ModInt that) {
    x = (unsigned long long)x * that.x % MOD;
    return *this;
  }
  ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
  ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
  ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  ModInt inverse() const {
    long long a = x, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  bool operator==(ModInt that) const { return x == that.x; }
  bool operator!=(ModInt that) const { return x != that.x; }
  ModInt operator-() const {
    ModInt t;
    t.x = x == 0 ? 0 : Mod - x;
    return t;
  }
};
template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) {
  st << a.get();
  return st;
};
template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
  ModInt<MOD> r = 1;
  while (k) {
    if (k & 1)
      r *= a;
    a *= a;
    k >>= 1;
  }
  return r;
}
template <typename T, int FAC_MAX> struct Comb {
  vector<T> fac, ifac;
  Comb() {
    fac.resize(FAC_MAX, 1);
    ifac.resize(FAC_MAX, 1);
    for (int i = 1; i < FAC_MAX; i++)
      fac[i] = fac[i - 1] * i;
    ifac[FAC_MAX - 1] = T(1) / fac[FAC_MAX - 1];
    for (int i = FAC_MAX - 2; i >= 1; i--)
      ifac[i] = ifac[i + 1] * T(i + 1);
  }
  T aPb(int a, int b) {
    if (b < 0 || a < b)
      return T(0);
    return fac[a] * ifac[a - b];
  }
  T aCb(int a, int b) {
    if (b < 0 || a < b)
      return T(0);
    return fac[a] * ifac[a - b] * ifac[b];
  }
  T nHk(int n, int k) {
    if (n == 0 && k == 0)
      return T(1);
    if (n <= 0 || k < 0)
      return 0;
    return aCb(n + k - 1, k);
  }
}; // nHk = (n+k-1)Ck : n is separator

using mint = ModInt<int(1e9 + 7)>;
using vm = vector<mint>;

map<ll, ll> enumpr(ll n) {
  map<ll, ll> V;
  for (ll i = 2; 1LL * i * i <= n; i++)
    while (n % i == 0)
      V[i]++, n /= i;
  if (n > 1)
    V[n]++;
  return V;
}

// 約数の列挙
vector<ll> divisors(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (n / i != i)
        res.push_back(n / i);
    }
  }
  return res;
}

void solve(long long N) {
  auto pr = divisors(N);
  dump(pr);
  set<Int> st;
  for (auto &&k : pr) {
    if (k == 1)
      continue;
    Int kj = 1;
    for (Int j = 1; N % kj == 0; kj *= k) {
      Int rest = N / kj;
      if (rest % k == 1LL && k > 1LL)
        st.insert(k);
    }
  }
  // if kj == 1
  Int ki = N - 1;
  auto div = divisors(ki);
  for (auto &&k : div)
    if (k > 1)
      st.insert(k);
  dump(st);
  cout << st.size() << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  long long N;
  scanf("%lld", &N);
  solve(N);
  return 0;
}