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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct prime {
  vector<int> f, prime_list;
  prime(int n = 1) : f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      f[i] = i;
      prime_list.pb(i);
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }

  bool is_prime(int a) { return f[a] == a; }

  vector<int> ve_prime(int a) {
    vector<int> res;
    while (a != 1) {
      res.pb(f[a]);
      a /= f[a];
    }
    return res;
  }

  vector<pair<int, int>> num_prime(int a) {
    vector<int> flist = ve_prime(a);
    if (flist.size() == 0)
      return {};
    vector<pii> res(1, {flist[0], 0});
    for (int p : flist) {
      if (res.back().fi == p)
        res.back().se++;
      else
        res.pb({p, 1});
    }
    return res;
  }
};

template <typename T> vector<T> divisor(T a) {
  vector<T> res;
  for (T i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      res.push_back(i);
      if (i != a / i)
        res.push_back(a / i);
    }
  }
  return res;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N;
  cin >> N;
  vl A = divisor(N);
  sort(all(A));
  int b = 0;
  rep(i, (int)(A.size())) if (A[i] <= 2000000) b = (int)A[i];

  ll cc = N / (ll)b;
  map<ll, ll> D;

  if (cc > 2000000) {
    D[cc]++;
    prime P(2000000);

    ve<pii> aa = P.num_prime(b);
    rep(i, int(aa.size())) D[aa[i].fi] += aa[i].se;

  } else {
    int c = (int)cc;

    prime P(2000000);

    ve<pii> aa = P.num_prime(c);
    ve<pii> bb = P.num_prime(b);
    rep(i, int(aa.size())) D[aa[i].fi] += aa[i].se;
    rep(i, int(bb.size())) D[bb[i].fi] += bb[i].se;
  }

  ll ans = 0;
  for (auto p : D) {
    ll k = 1;
    ll kk = p.se;
    while (kk - k >= 0) {
      ans++;
      kk -= k;
      k++;
    }
    // cout << p.fi csp p.se << endl;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct prime {
  vector<int> f, prime_list;
  prime(int n = 1) : f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      f[i] = i;
      prime_list.pb(i);
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }

  bool is_prime(int a) { return f[a] == a; }

  vector<int> ve_prime(int a) {
    vector<int> res;
    while (a != 1) {
      res.pb(f[a]);
      a /= f[a];
    }
    return res;
  }

  vector<pair<int, int>> num_prime(int a) {
    vector<int> flist = ve_prime(a);
    if (flist.size() == 0)
      return {};
    vector<pii> res(1, {flist[0], 0});
    for (int p : flist) {
      if (res.back().fi == p)
        res.back().se++;
      else
        res.pb({p, 1});
    }
    return res;
  }
};

template <typename T> vector<T> divisor(T a) {
  vector<T> res;
  for (T i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      res.push_back(i);
      if (i != a / i)
        res.push_back(a / i);
    }
  }
  return res;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N;
  cin >> N;
  vl A = divisor(N);
  sort(all(A));
  // outve(A);
  int b = 0;
  rep(i, (int)(A.size())) if (A[i] <= 1000000) b = (int)A[i];

  if (b == 1) {
    cout << 1 << endl;
    return 0;
  }

  ll cc = N / (ll)b;
  int c = (int)cc;

  prime P(2000000);

  ve<pii> aa = P.num_prime(c);
  ve<pii> bb = P.num_prime(b);

  // rep(i,int(aa.size())) cout << aa[i].fi csp aa[i].se << endl;
  // cout << endl;
  // rep(i,int(bb.size())) cout << bb[i].fi csp bb[i].se << endl;

  map<ll, ll> D;
  rep(i, int(aa.size())) D[aa[i].fi] += aa[i].se;
  rep(i, int(bb.size())) D[bb[i].fi] += bb[i].se;
  ll ans = 0;
  for (auto p : D) {
    ll k = 1;
    ll kk = p.se;
    // cout << kk << endl;
    while (kk - k >= 0) {
      ans++;
      kk -= k;
      k++;
    }
    // cout << p.fi csp p.se << endl;
  }
  cout << ans << endl;

  return 0;
}