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

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

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979323846

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll cnt_rep = 0;
ll powmod(ll N, ll P, ll M) {
  cnt_rep++;
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = powmod(N, P / 2, M);
    return t * t % M;
  }
  return (N * powmod(N, P - 1, M)) % M;
}

ll gcd(ll a, ll b) {
  ll resi, g, l;
  if (a > b) {
    g = a;
    l = b;
  } else if (b > a) {
    g = b;
    l = a;
  } else {
    return a;
  }
  resi = g % l;
  if (resi != 0) {
    return gcd(l, resi);
  } else {
    return l;
  }
}

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int ans = 0;
int N, M, Q;

template <typename T> void showall(vector<T> v) {
  rep(i, 0, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

template <typename T> ll com(T n, T m, T mod) {
  ll a = 1, b = 1, ans;
  if (n == m || m == 0) {
    ans = 1;
  } else {
    for (ll i = 0; i < m; i++) {
      a *= n - i;
      a %= mod;
      b *= i + 1;
      b %= mod;
    }
    ans = (a * powmod(b, mod - 2, mod)) % mod;
  }

  return ans;
}

bool IsPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

vector<long long> divisor(long long n) {
  vector<long long> ret;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end()); // 昇順に並べる
  return ret;
}

int main() {
  ll N, ans = 0;
  ;

  cin >> N;

  for (ll i = 2; i * i <= N; i++) {
    if (N % i != 0)
      continue;
    ll num = 0;
    while (true) {
      if (N % i == 0) {
        N /= i;
        num++;
      } else {
        break;
      }
    }

    ll cnt = 1;
    while (true) {
      if (num >= cnt) {
        num -= cnt;
        ans++;
        cnt++;
      } else {
        break;
      }
    }
  }

  if (N != 1)
    ans++;
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979323846

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll cnt_rep = 0;
ll powmod(ll N, ll P, ll M) {
  cnt_rep++;
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = powmod(N, P / 2, M);
    return t * t % M;
  }
  return (N * powmod(N, P - 1, M)) % M;
}

ll gcd(ll a, ll b) {
  ll resi, g, l;
  if (a > b) {
    g = a;
    l = b;
  } else if (b > a) {
    g = b;
    l = a;
  } else {
    return a;
  }
  resi = g % l;
  if (resi != 0) {
    return gcd(l, resi);
  } else {
    return l;
  }
}

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int ans = 0;
int N, M, Q;

template <typename T> void showall(vector<T> v) {
  rep(i, 0, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

template <typename T> ll com(T n, T m, T mod) {
  ll a = 1, b = 1, ans;
  if (n == m || m == 0) {
    ans = 1;
  } else {
    for (ll i = 0; i < m; i++) {
      a *= n - i;
      a %= mod;
      b *= i + 1;
      b %= mod;
    }
    ans = (a * powmod(b, mod - 2, mod)) % mod;
  }

  return ans;
}

std::vector<ll> Eratosthenes(const ll N) {
  std::vector<bool> is_prime(N + 1);
  for (ll i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  std::vector<ll> P;
  for (ll i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (ll j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}

int main() {
  ll N;

  cin >> N;

  set<ll> used;
  vector<ll> p;

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

  // 割れる素数を求める
  for (const auto &x : Eratosthenes(N)) {
    if (N % x == 0) {
      p.push_back(x);
    }
  }

  ll cnt = 0;

  ll e = 0;
  bool finish = false;

  while (!finish) {
    e++;

    for (auto &a : p) {
      ll z = (ll)pow(a, e);
      if (N % z == 0 && used.find(z) == used.end()) {
        N = N / z;
        used.insert(z);
        cnt++;
      }

      if (z > N)
        finish = true;
    }
  }

  cout << cnt << endl;
}