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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double PI = acos(-1);
const double EPS = 1e-10;

// 約数列挙
template <class T> vector<T> divisor(T x) {
  vector<T> res;
  for (T i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      res.push_back(i);
      if (i != x / i) {
        res.push_back(x / i);
      }
    }
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> ret2 = divisor(n);
  ll ans = 0;
  // cout<<ret2<<endl;
  for (auto itr : ret2) {
    ll k = itr;
    if (k < 2)
      continue;
    // cout<<k<<endl;
    ll now = 1;
    ll cnt = 0;
    ll nn = n;
    while (nn) {
      nn /= k;
      cnt++;
    }
    ll flag = 0;
    while (now <= n) {
      // cout<<now<<endl;
      flag++;
      if (n % now != 0)
        break;
      ll y = n / now;
      y--;
      if (y >= 0 && y % k == 0) {
        ll l = y / k;
        if (l >= 0) {
          // cout<<k<<' '<<now<<endl;
          ans++;
        }
      }
      if (flag < cnt)
        now *= k;
      else
        break;
    }
    // cout<<endl;
  }
  vector<ll> ret3 = divisor(n - 1);
  ans += (ll)ret3.size() - 1;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double PI = acos(-1);
const double EPS = 1e-10;

// 素因数分解
template <class T> map<T, T> prime_factor(T x) {
  map<T, T> ans;
  for (T i = 2; i * i <= x; i++) {
    while (x % i == 0) {
      x /= i;
      ans[i]++;
    }
  }
  if (x != 1) {
    ans[x] = 1;
  }
  return ans;
}

// 約数列挙
template <class T> vector<T> divisor(T x) {
  vector<T> res;
  for (T i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      res.push_back(i);
      if (i != x / i) {
        res.push_back(x / i);
      }
    }
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  map<ll, ll> ret = prime_factor(n);
  vector<ll> ret2 = divisor(n);
  ll ans = 0;
  // cout<<ret2<<endl;
  for (auto itr : ret2) {
    ll k = itr;
    if (k < 2)
      continue;
    // cout<<k<<endl;
    ll now = 1;
    while (now <= n) {
      // cout<<now<<endl;
      if (n % now != 0)
        break;
      ll y = n / now;
      y--;
      if (y >= 0 && y % k == 0) {
        ll l = y / k;
        if (l >= 0) {
          // cout<<k<<' '<<now<<endl;
          ans++;
        }
      }
      now *= k;
    }
    // cout<<endl;
  }
  vector<ll> ret3 = divisor(n - 1);
  ans += (ll)ret3.size() - 1;
  cout << ans << endl;
}