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

#include <cmath>
#include <iostream>
int main() {
  long long int a, b, count = 0;
  std::cin >> a;
  b = a;
  bool hantei[2000001]; // 十分大きなサイズを確保します
  //     for (long long int i = 0; i<= 1000000; ++i) hantei[i]=false; //
  //     とりあえず全部ふるいに入れます
  for (long long int i = 2; i <= sqrt(a) + 100000; ++i) {
    if (!hantei[i]) { // 素数 i を発見したら
      for (long long int j = i * 2; j <= sqrt(a) + 100000; j += i) {
        hantei[j] = true; // i の倍数をふるい落とす
      }
    }
  }
  //    std::cout<<is_prime(0)<<std::endl;
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      long long int j = i;
      while (true) {
        if (b == 1 || b < i) {
          break;
        }
        if (b % j != 0)
          break;
        b = b / j;
        count++;
        j = j * i;
      }
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  for (long long int i = 2; i <= sqrt(a) + 100000; i++) {
    if (!hantei[i]) {
      if (b % i == 0)
        b = b / i;
    }
  }
  if (b != 1 && a != 1 && count != 0)
    count++;
  if (count == 0 && a != 1)
    count++;
  std::cout << count << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
int main() {
  long long int a, b, count = 0;
  std::cin >> a;
  b = a;
  bool hantei[1000001]; // 十分大きなサイズを確保します
  //     for (long long int i = 0; i<= 1000000; ++i) hantei[i]=false; //
  //     とりあえず全部ふるいに入れます
  for (long long int i = 2; i <= sqrt(a) + 100; ++i) {
    if (!hantei[i]) { // 素数 i を発見したら
      for (long long int j = i * 2; j <= sqrt(a) + 100; j += i) {
        hantei[j] = true; // i の倍数をふるい落とす
      }
    }
  }
  //    std::cout<<is_prime(0)<<std::endl;
  for (long long int i = 2; i <= sqrt(a) + 100; i++) {
    if (!hantei[i]) {
      long long int j = i;
      while (true) {
        if (b == 1 || b < i) {
          break;
        }
        if (b % j != 0)
          break;
        b = b / j;
        count++;
        j = j * i;
      }
    }
  }
  if (count == 0 && a != 1)
    count++;
  std::cout << count << std::endl;
  return 0;
}