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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  ll N;
  cin >> N;
  map<ll, int> P;
  ll N2 = N;
  N--;
  rep(q, 100) {
    rep(i, sqrt(N) + 1) {
      if (N % (i + 2) == 0) {
        P[i + 2]++;
        N /= (i + 2);
        break;
      }
    }
  }
  if (N != 1)
    P[N]++;
  ll ans = 1;
  auto itr = P.begin();
  rep(i, P.size()) {
    ans *= (itr->second + 1);
    itr++;
  }
  map<ll, int> P2;
  N = N2;
  rep(q, 50) {
    rep(i, sqrt(N2) + 1) {
      if (N2 % (i + 2) == 0) {
        P2[i + 2]++;
        N2 /= (i + 2);
        break;
      }
    }
  }
  if (N2 != 1)
    P2[N2]++;
  VVL D(P2.size() + 1);
  auto itr2 = P2.begin();
  D[0].pb(1);
  rep(i, P2.size()) {
    ll tmp = itr2->first;
    rep(j, itr2->second) {
      rep(k, D[i].size()) { D[i + 1].pb(tmp * D[i][k]); }
      tmp *= itr2->first;
    }
    rep(j, D[i].size()) { D[i + 1].pb(D[i][j]); }
    itr2++;
  }
  rep(i, D[P2.size()].size()) {
    ll X = D[P2.size()][i];
    ll Y = 0;
    ll N3 = N;
    if (X != 1 and X != N3)
      rep(q, 100) {
        if (N3 % X == 0)
          N3 /= X;
        else
          N3 %= X;
        if (N3 == 1) {
          Y = 1;
          break;
        }
      }
    if (Y) {
      ans++;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  ll N;
  cin >> N;
  map<ll, int> P;
  ll N2 = N;
  N--;
  rep(q, 50) {
    rep(i, sqrt(N) + 1) {
      if (N % (i + 2) == 0) {
        P[i + 2]++;
        N /= (i + 2);
      }
    }
  }
  if (N != 1)
    P[N]++;
  int ans = 1;
  auto itr = P.begin();
  rep(i, P.size()) {
    ans *= (itr->second + 1);
    itr++;
  }
  map<ll, int> P2;
  N = N2;
  rep(q, 50) {
    rep(i, sqrt(N2) + 1) {
      if (N2 % (i + 2) == 0) {
        P2[i + 2]++;
        N2 /= (i + 2);
      }
    }
  }
  if (N2 != 1)
    P2[N2]++;
  VVL D(P2.size() + 1, VL());
  itr = P2.begin();
  D[0].pb(1);
  rep(i, P2.size()) {
    ll tmp = itr->first;
    rep(j, itr->second) {
      rep(k, D[i].size()) { D[i + 1].pb(tmp * D[i][k]); }
      tmp *= itr->first;
    }
    rep(j, D[i].size()) { D[i + 1].pb(D[i][j]); }
    itr++;
  }
  rep(i, D[P2.size()].size()) {
    int X = D[P2.size()][i];
    int Y = 0;
    int N3 = N;
    rep(q, 50) {
      if (N3 % X == 0)
        N3 /= X;
      else
        N3 %= X;
      if (N3 == 1) {
        Y = 1;
        break;
      }
    }
    if (Y) {
      ans++;
    }
  }
  cout << ans - 1 << endl;
}