Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = a; i < (int)(b); ++i)
#define forin(i, n) for (auto &i : (n))
#define make_unique(x) (x).erase(unique(all((x))), (x).end())
#define hbar                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
template <class T> T mgcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  hbar;
  ll a, b;
  cin >> a >> b;
  ll n = mgcd(a, b), ans = 1;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0)
        n /= i;
      ++ans;
    }
  }
  if (n > 1)
    ++ans;

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = a; i < (int)(b); ++i)
#define forin(i, n) for (auto &i : (n))
#define make_unique(x) (x).erase(unique(all((x))), (x).end())
#define hbar                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
template <class T> T mgcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  hbar;
  ll a, b;
  cin >> a >> b;
  ll n = mgcd(a, b), ans = 1;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0)
        n /= i;
      ++ans;
    }
  }
  if (n > 1)
    ++ans;

  cout << ans << '\n';

  return 0;
}