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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define timer_start()                                                          \\
  chrono::system_clock::time_point start, end;                                 \\
  start = chrono::system_clock::now();
#define timer_wrap()                                                           \\
  end = chrono::system_clock::now();                                           \\
  double time = static_cast<double>(                                           \\
      chrono::duration_cast<chrono::microseconds>(end - start).count() /       \\
      1000.0);                                                                 \\
  printf("%lf[ms]\\n", time);                                                   \\
  start = end;
#define optimize_cin()                                                         \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
using namespace std;
using ll = long long;
#define INF 1000000000   // 10^9
#define INFINF LLONG_MAX // 2^63-1
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;
}
// ########## template end ##########

#ifdef DEBUG_ON
void test_all();
#endif

const int N = 10000001;
vector<bool> is_prime(N, true);
vector<bool> forans(N, false);
vector<int> pr;

void prime_table() {
  is_prime.at(0) = false;
  is_prime.at(1) = false;
  for (int i = 0; i < N; i++) {
    if (!is_prime.at(i)) {
      continue;
    }
    for (int j = i + i; j < N; j += i) {
      is_prime.at(j) = false;
    }
  }
}
void build_ans() {
  ll tmp = 0;
  for (int i = 2; i < N / 2; i++) {
    if (!is_prime[i]) {
      continue;
    }
    for (int j = 1; j < N; j++) {
      tmp = pow(i, j);
      if (tmp > N && tmp > 0) {
        break;
      }
      forans[tmp] = true;
    }
  }
}
bool isPrime(ll 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;
}
void resolve() {
  prime_table();
  build_ans();
  ll n;
  cin >> n;
  ll ans = 0;
  if (isPrime(n)) {
    cout << 1 << endl;
    return;
  }
  for (ll i = 2; i < N; i++) {
    if (!forans[i]) {
      continue;
    }
    if (i > n) {
      break;
    }
    if (n % i == 0) {
      ans++;
      n = n / i;
    }
  }
  if (n > N) {
    ans++;
  }
  cout << ans << endl;
}

int main() {
#ifdef DEBUG_ON
  test_all();
#else
  resolve();
#endif
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define timer_start()                                                          \\
  chrono::system_clock::time_point start, end;                                 \\
  start = chrono::system_clock::now();
#define timer_wrap()                                                           \\
  end = chrono::system_clock::now();                                           \\
  double time = static_cast<double>(                                           \\
      chrono::duration_cast<chrono::microseconds>(end - start).count() /       \\
      1000.0);                                                                 \\
  printf("%lf[ms]\\n", time);                                                   \\
  start = end;
#define optimize_cin()                                                         \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
using namespace std;
using ll = long long;
#define INF 1000000000   // 10^9
#define INFINF LLONG_MAX // 2^63-1
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;
}
// ########## template end ##########

#ifdef DEBUG_ON
void test_all();
#endif

vector<bool> is_prime;
vector<bool> forans;
vector<int> pr;

void prime_table(int n) {
  is_prime = vector<bool>(n, true);
  is_prime.at(0) = false;
  is_prime.at(1) = false;
  for (int i = 0; i < n; i++) {
    if (!is_prime.at(i)) {
      continue;
    }
    for (int j = i + i; j < n; j += i) {
      is_prime.at(j) = false;
    }
  }
}
void build_ans(int n) {
  forans = vector<bool>(n, false);
  ll tmp = 0;
  for (int i = 2; i < n / 2; i++) {
    if (!is_prime[i]) {
      continue;
    }
    for (int j = 1; j < n; j++) {
      tmp = pow(i, j);
      if (tmp > n && tmp > 0) {
        break;
      }
      forans[tmp] = true;
    }
  }
}
bool isPrime(ll 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;
}
void resolve() {
  ll n;
  cin >> n;
  prime_table(pow(10, 6) + 1);
  build_ans(pow(10, 6) + 1);
  ll ans = 0;
  if (isPrime(n)) {
    cout << 1 << endl;
    return;
  }
  for (ll i = 2; i <= n; i++) {
    if (!forans[i]) {
      continue;
    }
    if (i > n) {
      break;
    }
    if (n % i == 0) {
      ans++;
      n = n / i;
    }
  }
  cout << ans << endl;
}

int main() {
#ifdef DEBUG_ON
  test_all();
#else
  resolve();
#endif
  return 0;
}