Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define IFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RPT(i, a, b) for (int i = (a); i < ((a) + (b)); ++i)
#define IRPT(i, a, b) for (int i = ((a) + (b)-1); i >= (a); --i)

void solve() {
  ll a, b;
  cin >> a >> b;
  if (a > b)
    swap(a, b);
  vector<ll> prime;
  for (ll i = 2; i < 1000010; ++i) {
    bool yprime = true;
    for (auto x : prime) {
      if (i % x == 0) {
        yprime = false;
        break;
      }
      if (x * x > i)
        break;
    }
    if (yprime)
      prime.push_back(i);
  }
  {
    bool yprime = true;
    for (auto x : prime) {
      if (a % x == 0) {
        yprime = false;
        break;
      }
    }
    if (yprime)
      prime.push_back(a);
    yprime = true;
    for (auto x : prime) {
      if (b % x == 0) {
        yprime = false;
        break;
      }
    }
    if (yprime)
      prime.push_back(b);
  }
  vector<ll> p(prime.size(), 0);
  {
    for (int i = 0; i < prime.size(); ++i) {
      ll cnt = 0;
      while (a % prime[i] == 0) {
        a /= prime[i];
        cnt++;
      }
      p[i] = cnt;
      if (a == 1)
        break;
    }
    if (a > 1) {
      prime.push_back(a);
      p.push_back(1);
    }
    for (int i = 0; i < prime.size(); ++i) {
      ll cnt = 0;
      while (b % prime[i] == 0) {
        b /= prime[i];
        cnt++;
      }
      p[i] = min(p[i], cnt);
    }
  }
  ll ans = 1;
  // for(int i=0; i<p.size(); ++i) if(p[i]>0) cout << prime[i] << "," << p[i] <<
  // " ";
  for (auto x : p) {
    ans += (x > 0);
  }
  cout << ans
       << '\n'; // cout << 359120361600%12457 << " " << 390691501200%12457;
}

int main() {
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define IFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RPT(i, a, b) for (int i = (a); i < ((a) + (b)); ++i)
#define IRPT(i, a, b) for (int i = ((a) + (b)-1); i >= (a); --i)

void solve() {
  ll a, b;
  cin >> a >> b;
  if (a == 1 || b == 1) {
    cout << 1 << '\n';
    return;
  }
  if (a > b)
    swap(a, b);
  vector<ll> prime;
  for (ll i = 2; i < 1000010; ++i) {
    bool yprime = true;
    for (auto x : prime) {
      if (i % x == 0) {
        yprime = false;
        break;
      }
      if (x * x > i)
        break;
    }
    if (yprime)
      prime.push_back(i);
  }
  {
    bool yprime = true;
    for (auto x : prime) {
      if (a % x == 0) {
        yprime = false;
        break;
      }
    }
    if (yprime)
      prime.push_back(a);
    yprime = true;
    for (auto x : prime) {
      if (b % x == 0) {
        yprime = false;
        break;
      }
    }
    if (yprime)
      prime.push_back(b);
  }
  vector<ll> p(prime.size(), 0);
  {
    for (int i = 0; i < prime.size(); ++i) {
      ll cnt = 0;
      while (a % prime[i] == 0) {
        a /= prime[i];
        cnt++;
      }
      p[i] = cnt;
      if (a == 1)
        break;
    }
    if (a > 1) {
      prime.push_back(a);
      p.push_back(1);
    }
    for (int i = 0; i < prime.size(); ++i) {
      ll cnt = 0;
      while (b % prime[i] == 0) {
        b /= prime[i];
        cnt++;
      }
      p[i] = min(p[i], cnt);
    }
  }
  ll ans = 1;
  // for(int i=0; i<p.size(); ++i) if(p[i]>0) cout << prime[i] << "," << p[i] <<
  // " ";
  for (auto x : p) {
    ans += (x > 0);
  }
  cout << ans
       << '\n'; // cout << 359120361600%12457 << " " << 390691501200%12457;
}

int main() {
  solve();
  return 0;
}