Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h> // ver2.1.4
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(), (v).end()
#define Vi vector<int>
#define Pii pair<int, int>
#define Tiii tuple<int, int, int>
#define PQi priority_queue<int>
#define PQir priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define sortr(v) sort(v, greater<>())
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i > b; i--)
#define repreq(i, a, b) for (int i = a; i >= b; i--)
#define leng(n) (int)(log10(n) + 1)
#define dem(a, b) ((a + b - 1) / (b))
#define Vout(a)                                                                \
  rep(i, 0, a.size() - 1) cout << a[i] << ' ';                                 \
  cout << a.back() << endl
#define INF 9000000000000000000 //  9.0 * 10^18
#define MOD 1000000007          //  10^9 + 7
using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } //  最大公約数gcd
int lcm(int a, int b) { return a / gcd(a, b) * b; }     //  最小公倍数lcm

int mypow(int x, int n, int m) { //  高速累乗x^n(mod m)  O(log(n))
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return mypow(x * x % m, n / 2, m);
  else
    return x * mypow(x, n - 1, m) % m;
}

bool prime(int n) { //  素数判定
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  double s = sqrt(n);
  for (int i = 3; i <= s; i += 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

#define N 1000000
int arr[N];

Vi Eratosthenes() {
  Vi a(0);
  for (int i = 0; i < N; i++) {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(N); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < N; j++) {
        arr[i * (j + 2)] = 0;
      }
    }
  }

  for (int i = 2; i < N; i++) {
    if (arr[i]) {
      a.pb(i); // cout << i << endl;
    }
  }
  return a;
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  int n, m;
  cin >> n >> m;
  int now = gcd(n, m);

  /*Vi so(0);
  repeq(i,2,1000000){
    if(prime(i)) so.pb(i);
  }*/
  Vi so;
  so = Eratosthenes();
  int ans = 1;
  rep(i, 0, so.size()) {
    if (now % so[i] == 0) {
      ans++;
      while (now % so[i] == 0)
        now %= so[i];
    }
  }

  if (prime(now))
    ans++;

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h> // ver2.1.4
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(), (v).end()
#define Vi vector<int>
#define Pii pair<int, int>
#define Tiii tuple<int, int, int>
#define PQi priority_queue<int>
#define PQir priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define sortr(v) sort(v, greater<>())
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i > b; i--)
#define repreq(i, a, b) for (int i = a; i >= b; i--)
#define leng(n) (int)(log10(n) + 1)
#define dem(a, b) ((a + b - 1) / (b))
#define Vout(a)                                                                \
  rep(i, 0, a.size() - 1) cout << a[i] << ' ';                                 \
  cout << a.back() << endl
#define INF 9000000000000000000 //  9.0 * 10^18
#define MOD 1000000007          //  10^9 + 7
using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } //  最大公約数gcd
int lcm(int a, int b) { return a / gcd(a, b) * b; }     //  最小公倍数lcm

int mypow(int x, int n, int m) { //  高速累乗x^n(mod m)  O(log(n))
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return mypow(x * x % m, n / 2, m);
  else
    return x * mypow(x, n - 1, m) % m;
}

bool prime(int n) { //  素数判定
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  double s = sqrt(n);
  for (int i = 3; i <= s; i += 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

#define N 1000000
int arr[N];

Vi Eratosthenes() {
  Vi a(0);
  for (int i = 0; i < N; i++) {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(N); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < N; j++) {
        arr[i * (j + 2)] = 0;
      }
    }
  }

  for (int i = 2; i < N; i++) {
    if (arr[i]) {
      a.pb(i); // cout << i << endl;
    }
  }
  return a;
}

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  int n, m;
  cin >> n >> m;
  int now = gcd(n, m);

  /*Vi so(0);
  repeq(i,2,1000000){
    if(prime(i)) so.pb(i);
  }*/
  Vi so;
  so = Eratosthenes();
  int ans = 1;
  rep(i, 0, so.size()) {
    if (now % so[i] == 0) {
      ans++;
      while (now % so[i] == 0)
        now /= so[i];
    }
  }

  if (prime(now))
    ans++;

  cout << ans << endl;

  return 0;
}