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

#include <bits/stdc++.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define endl "\\n"
#define ll long long
#define int long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cin.exceptions(cin.failbit);                                                 \\
  cout.tie(NULL);
using namespace std;
bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i += 6) {
    if ((n % i == 0) || (n % (i + 2) == 0))
      return false;
  }
  return true;
}
map<int, int> mp;
void primeFactors(int n) {
  // Print the number of 2s that divide n
  while (n % 2 == 0) {
    mp[2]++;
    // cout << 2 << " ";
    n = n / 2;
  }

  // n must be odd at this point. So we can skip
  // one element (Note i = i +2)
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    // While i divides n, print i and divide n
    while (n % i == 0) {
      mp[i]++;
      // cout << i << " ";
      n = n / i;
    }
  }

  // This condition is to handle the case when n
  // is a prime number greater than 2
  if (n > 2)
    mp[n]++;
  // cout << n << " ";
}
int32_t main() {
  int n;
  cin >> n;
  primeFactors(n);
  int cnt = 0;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    int x = (*it).second;
    int y = (sqrt(8 * x + 1) - 1) / 2;
    cnt += y;
  }
  cout << cnt;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define endl "\\n"
#define ll long long
#define int long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cin.exceptions(cin.failbit);                                                 \\
  cout.tie(NULL);
using namespace std;
bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i += 6) {
    if ((n % i == 0) || (n % (i + 2) == 0))
      return false;
  }
  return true;
}
bool prime[1000005 + 1];
void SieveOfEratosthenes(int n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }

  // Print all prime numbers
  /* for (int p=2; p<=n; p++)
      if (prime[p])
         cout << p << " ";
 */
}
// int valid(int n)
//{
//	int raj=n;
//	for(int i=2;i<=1000000;i++)
//	{
//		n=raj;
//		if(prime[i] && n%i==0)
//		{
//			while(n>=2)
//			{
//				if(n%i==0)
//				n=n/i;
//				else
//				break;
//
//			}
//		}
//		if(n==1)
//		return 1;
//	}
//	return 0;
// }
//// Driver Program to test above function
int a[1000005] = {0};
int32_t main() {
  int n;
  cin >> n;
  prime[0] = false;
  prime[1] = false;

  SieveOfEratosthenes(1000004);
  int cnt = 0, tt = 1e6;
  for (int i = 2; i <= 1000000; i++) {
    if (prime[i]) {
      a[i] = 1;
      int rr = i;
      while (rr <= tt) {
        a[rr] = 1;
        rr *= i;
      }
    }
  }

  /*for(int i=0;i<100;i++)
  {
          cout << a[i] << " ";
  }
  cout << endl;
  */
  set<int> s;
  while (n >= 2) {
    vector<int> v;
    for (int i = 1; i <= sqrt(n); i++) {
      if (n % i == 0) {
        if (n / i == i)
          v.push_back(i);
        else {
          v.push_back(i);
          v.push_back(n / i);
        }
      }
    }
    int d = INT_MAX;
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
      if (a[v[i]] == 1 && s.find(v[i]) == s.end()) {
        d = v[i];
        break;
      }
    }
    if (d != INT_MAX) {
      cnt++;
      n /= d;
      s.insert(d);
      // cout << d << " ";
    } else
      break;
  }
  cout << cnt;
}