Problem p02900 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
template <class T> struct Sieve {
  T n;
  vector<T> f, primes;
  Sieve(T n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }
  // 素数かどうかのboolを返す
  bool isPrime(T x) { return f[x] == x; }
  // 昇順の素因数リストを返す
  vector<T> factorList(T x) {
    vector<T> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  // 昇順の(素因数、その数)のpairを返す
  vector<pair<T, T>> factor(T x) {
    vector<T> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<T, T>> res(1, make_pair(fl[0], 0));
    for (T p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
  ll a, b;
  cin >> a >> b;
  ll gcdab = gcd(max(a, b), min(a, b));
  Sieve<ll> s(gcdab);
  vector<pair<ll, ll>> fac = s.factor(gcdab);
  int ans = fac.size() + 1;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
template <class T> struct Sieve {
  T n;
  vector<T> f, primes;
  Sieve(T n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }
  // 素数かどうかのboolを返す
  bool isPrime(T x) { return f[x] == x; }
  // 昇順の素因数リストを返す
  vector<T> factorList(T x) {
    vector<T> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  // 昇順の(素因数、その数)のpairを返す
  vector<pair<T, T>> factor(T x) {
    vector<T> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<T, T>> res(1, make_pair(fl[0], 0));
    for (T p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
  ll a, b;
  cin >> a >> b;
  ll gcdab = gcd(max(a, b), min(a, b));
  int mx = sqrt(gcdab) + 1;
  int ans = 1;
  for (int i = 2; i <= mx; i++) {
    if (gcdab % i == 0)
      ans++;
    while (gcdab % i == 0)
      gcdab /= i;
    if (gcdab == 1)
      break;
  }
  if (gcdab != 1)
    ans++;
  cout << ans << endl;

  return 0;
}