Problem p02900 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 100000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
using namespace std;

bool is_prime[100000000 + 1]; // Up to 1e8 + 1
template <typename T>
vector<T> Eratosthenes(const T N) { // return primes up to N
  vector<T> P;
  for (int i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);

      // if (P.size() >= N) break;
    }
  }
  return P;
}

template <typename T> T gcd(T a, T b) {
  if (a > b) {
    T c = a;
    a = b;
    b = c;
  }
  if (a == 0) {
    return b;
  }

  return gcd(b % a, a);
}

int main() {
  ll A, B;
  cin >> A >> B;
  vector<ll> Erast = Eratosthenes(static_cast<ll>(1000004));
  // cout << *(Erast.end()-1) << endl;
  ll cnt = 1;

  for (int i = 0; Erast[i] <= min({A, B, static_cast<ll>(1000004)}); i++) {
    if (A % Erast[i] == 0 or B % Erast[i] == 0) {
      if (A % Erast[i] == 0 and B % Erast[i] == 0)
        cnt++;
      while (A % Erast[i] == 0) {
        A /= Erast[i];
      }

      while (B % Erast[i] == 0) {
        B /= Erast[i];
      }
    }
  }
  if (A == B and A > 1) {
    cnt++;
  }
  cout << cnt << endl;
}


Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 100000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
using namespace std;

bool is_prime[100000000 + 1]; // Up to 1e8 + 1
template <typename T>
vector<T> Eratosthenes(const T N) { // return primes up to N
  vector<T> P;
  for (int i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);

      // if (P.size() >= N) break;
    }
  }
  return P;
}

template <typename T> T gcd(T a, T b) {
  if (a > b) {
    T c = a;
    a = b;
    b = c;
  }
  if (a == 0) {
    return b;
  }

  return gcd(b % a, a);
}

int main() {
  ll A, B;
  cin >> A >> B;
  vector<ll> Erast = Eratosthenes(static_cast<ll>(1000004));
  // cout << *(Erast.end()-1) << endl;
  ll cnt = 1;

  for (int i = 0; i < Erast.size(); i++) {
    if (A % Erast[i] == 0 or B % Erast[i] == 0) {
      if (A % Erast[i] == 0 and B % Erast[i] == 0)
        cnt++;
      while (A % Erast[i] == 0) {
        A /= Erast[i];
      }

      while (B % Erast[i] == 0) {
        B /= Erast[i];
      }
    }
  }
  if (A == B and A > 1) {
    cnt++;
  }
  cout << cnt << endl;
}