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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define mod 1000000007
int getTheAnswer() { return 42; }

using namespace std;
using ll = long long;
int a[1000005];
class DDivGame {
public:
  bool check(ll a, vector<int> &v) {
    for (int i = 0; i < v.size() && v[i] <= a; i++) {
      if (a % v[i] == 0) {
        while (a % v[i] == 0)
          a /= v[i];
        if (a != 1)
          return false;
        return true;
      }
    }
    return true;
  }
  void sieve(vector<int> &v) {
    a[0] = a[1] = 1;
    for (ll i = 2; i < 1000005; i++) {
      if (a[i] == 0) {
        v.pb(i);
        for (ll j = i * i; j < 1000005; j += i)
          a[j] = 1;
      }
    }
  }
  void solve(std::istream &cin, std::ostream &cout) {
    ll n;
    cin >> n;
    vector<ll> v;
    for (int i = 2; i < sqrt(n) + 1; i++)
      if (n % i == 0) {
        v.pb(i);
        v.pb(n / i);
      }
    if (v.size() == 0) {
      if (n == 1)
        cout << 0;
      else
        cout << 1 << endl;
      return;
    }
    sort(v.begin(), v.end());
    vector<int> fact;
    sieve(fact);
    int cnt = 0;
    for (int i = 0; i < v.size() && n > 1; i++) {
      if ((n % v[i] == 0) && check(v[i], fact)) {
        //                cout<<"val="<<v[i]<<endl;
        //                cout<<"n="<<n<<endl;
        n /= v[i];
        cnt++;
      }
    }
    cout << cnt;
  }
};

int main() {
  DDivGame solver;
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: WA

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define mod 1000000007
int getTheAnswer() { return 42; }

using namespace std;
using ll = long long;
int a[1000005];
class DDivGame {
public:
  bool check(ll a, vector<int> &v) {
    for (int i = 0; i < v.size() && v[i] <= a; i++) {
      if (a % v[i] == 0) {
        while (a % v[i] == 0)
          a /= v[i];
        if (a != 1)
          return false;
        return true;
      }
    }
  }
  void sieve(vector<int> &v) {
    a[0] = a[1] = 1;
    for (ll i = 2; i < 1000005; i++) {
      if (a[i] == 0) {
        v.pb(i);
        for (ll j = i * i; j < 1000005; j += i)
          a[j] = 1;
      }
    }
  }
  void solve(std::istream &cin, std::ostream &cout) {
    ll n;
    cin >> n;
    vector<ll> v;
    for (int i = 2; i < sqrt(n) + 1; i++)
      if (n % i == 0) {
        v.pb(i);
        v.pb(n / i);
      }
    if (v.size() == 0) {
      if (n == 1)
        cout << 0;
      else
        cout << 1 << endl;
      return;
    }
    sort(v.begin(), v.end());
    vector<int> fact;
    sieve(fact);
    int cnt = 0;
    for (int i = 0; i < v.size() && n > 1; i++) {
      if ((n % v[i] == 0) && check(v[i], fact)) {
        //                cout<<"val="<<v[i]<<endl;
        //                cout<<"n="<<n<<endl;
        n /= v[i];
        cnt++;
      }
    }
    cout << cnt;
  }
};

int main() {
  DDivGame solver;
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}