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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, d, n) for (int i = (d); i < (n); ++i)
#define rrep(i, n) for (int i = (n - 1); i >= 0; --i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int long long
template <typename T> using vv = vector<vector<T>>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using ll = long long;
constexpr ll INF = 1LL << 60;

bool is_prime(int x) {
  if (x < 2)
    return false;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0)
      return false;
  }
  return true;
}

class Prime {
public:
  int n;
  vector<int> factor, primes;
  // エラストテネスの篩
  Prime(int n = 100000) : n(n), factor(n + 1) {
    factor[0] = factor[1] = -1;
    for (int i = 2; i <= n; ++i) {
      if (factor[i]) {
        continue;
      }
      primes.push_back(i);
      factor[i] = i;
      for (int j = i * i; j <= n; j += i) {
        if (!factor[j]) {
          factor[j] = i;
        }
      }
    }
  }
  // 素数判定
  bool is_prime(int x) { return factor[x] == x; }
  // 約数列挙
  vector<int> factorList(int x) {
    vector<int> ret;
    while (x != 1) {
      ret.push_back(factor[x]);
      x /= factor[x];
    }
    return ret;
  }
  // 素因数分解
  vector<P> factorize(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0) {
      return {};
    }
    vector<P> res(1, P(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};

signed main() {

  int n;
  cin >> n;

  if (is_prime(n)) {
    cout << 1 << endl;
    return 0;
  }
  map<int, int> mp;
  Prime prime(1000000);
  if (n > 1000000) {
    rep(i, 1000000) {
      if (prime.is_prime(i) && n % i == 0) {
        mp[i] = 1;
        n /= i;
        if (n < 1000000) {
          break;
        }
      }
    }
  }

  for (int i = 2; i <= n; ++i) {
    if (i >= 1000000) {
      cout << mp.size() + 1 << endl;
      return 0;
    }
    if (!prime.is_prime(i)) {
      continue;
    }
    for (int j = i; j <= n; j *= i) {
      if (n % j == 0) {
        // cout << j << endl;
        if (mp[j] == 1) {
          continue;
        }
        mp[j] = 1;
        n /= j;
      }
    }
  }
  cout << mp.size() << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, d, n) for (int i = (d); i < (n); ++i)
#define rrep(i, n) for (int i = (n - 1); i >= 0; --i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int long long
template <typename T> using vv = vector<vector<T>>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using ll = long long;
constexpr ll INF = 1LL << 60;

bool is_prime(int x) {
  if (x < 2)
    return false;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0)
      return false;
  }
  return true;
}

class Prime {
public:
  int n;
  vector<int> factor, primes;
  // エラストテネスの篩
  Prime(int n = 100000) : n(n), factor(n + 1) {
    factor[0] = factor[1] = -1;
    for (int i = 2; i <= n; ++i) {
      if (factor[i]) {
        continue;
      }
      primes.push_back(i);
      factor[i] = i;
      for (int j = i * i; j <= n; j += i) {
        if (!factor[j]) {
          factor[j] = i;
        }
      }
    }
  }
  // 素数判定
  bool is_prime(int x) { return factor[x] == x; }
  // 約数列挙
  vector<int> factorList(int x) {
    vector<int> ret;
    while (x != 1) {
      ret.push_back(factor[x]);
      x /= factor[x];
    }
    return ret;
  }
  // 素因数分解
  vector<P> factorize(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0) {
      return {};
    }
    vector<P> res(1, P(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};

signed main() {

  int n;
  cin >> n;

  if (is_prime(n)) {
    cout << 1 << endl;
    return 0;
  }
  map<int, int> mp;
  Prime prime(10000000);
  if (n > 1000000) {
    rep(i, 1000000) {
      if (prime.is_prime(i) && n % i == 0) {
        mp[i] = 1;
        n /= i;
        if (n < 1000000) {
          break;
        }
      }
    }
  }

  for (int i = 2; i <= n; ++i) {
    if (!prime.is_prime(i)) {
      continue;
    }
    for (int j = i; j <= n; j *= i) {
      if (n % j == 0) {
        // cout << j << endl;
        if (mp[j] == 1) {
          continue;
        }
        mp[j] = 1;
        n /= j;
      }
    }
  }
  cout << mp.size() << endl;

  return 0;
}