Problem p02900 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

int64_t gcd(int64_t a, int64_t b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

// sqrt(n) までの素数を求める。1000014129 (31623 * 31623) まで対応
void create_primes(int64_t n, bitset<1000001> &primes) {
  primes.flip();

  int64_t u_b = sqrt(n + 1);
  assert(u_b < 31624);

  {
    for (int64_t j = 4; j <= u_b; j += 2) {
      primes.reset(j);
    }
  }
  for (int64_t i = 3; i <= u_b; i += 2) {
    if (!primes.test(i))
      continue;

    for (int64_t j = i * 2; j <= u_b; j += i) {
      primes.reset(j);
    }
  }
}

struct FactorInfo {
  int64_t v;
  int64_t c;
};

// n の素因数分解を行う。
bitset<1000001> primes;
vector<FactorInfo> make_factors(int64_t n) {
  create_primes(n, primes);

  int64_t v = n;
  int64_t limit = sqrt(n + 1);

  vector<FactorInfo> ans;
  FOR_LE(i, 2, limit) {
    if (!primes.test(i))
      continue;

    int64_t count = 0;
    while (v % i == 0) {
      v /= i;
      count++;
    }

    if (count != 0) {
      ans.push_back({i, count});
    }
  }

  if (v != 1) {
    ans.push_back({v, 1});
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int64_t a, b;
  cin >> a >> b;

  int64_t g = gcd(a, b);
  vector<FactorInfo> factors = make_factors(g);

  cout << factors.size() + 1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

int64_t gcd(int64_t a, int64_t b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

// sqrt(n) までの素数を求める。1000014129 (31623 * 31623) まで対応
void create_primes(int64_t n, bitset<1000001> &primes) {
  primes.flip();

  int64_t u_b = sqrt(n + 1);
  assert(u_b < 1000002);

  {
    for (int64_t j = 4; j <= u_b; j += 2) {
      primes.reset(j);
    }
  }
  for (int64_t i = 3; i <= u_b; i += 2) {
    if (!primes.test(i))
      continue;

    for (int64_t j = i * 2; j <= u_b; j += i) {
      primes.reset(j);
    }
  }
}

struct FactorInfo {
  int64_t v;
  int64_t c;
};

// n の素因数分解を行う。
bitset<1000001> primes;
vector<FactorInfo> make_factors(int64_t n) {
  create_primes(n, primes);

  int64_t v = n;
  int64_t limit = sqrt(n + 1);

  vector<FactorInfo> ans;
  FOR_LE(i, 2, limit) {
    if (!primes.test(i))
      continue;

    int64_t count = 0;
    while (v % i == 0) {
      v /= i;
      count++;
    }

    if (count != 0) {
      ans.push_back({i, count});
    }
  }

  if (v != 1) {
    ans.push_back({v, 1});
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int64_t a, b;
  cin >> a >> b;

  int64_t g = gcd(a, b);
  vector<FactorInfo> factors = make_factors(g);

  cout << factors.size() + 1 << endl;

  return 0;
}