Problem p02900 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mod 1000000007
#define MAX 1000000000000000
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (i = (ll)a; i < (ll)b; i++)
#define revrep(i, a, b) for (i = (ll)a; i >= (ll)b; i--)
#define ii pair<ll, ll>
#define MP make_pair
#define pb push_back
#define f first
#define se second
#define ll long long int
#define vi vector<ll>
ll modexp(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
#define rs resize
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    OST;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const ll N = 1e2 + 5;
ll i, j, a, b, ans;
vi v;
ll p[N];
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> a >> b;
  a = __gcd(a, b);
  for (i = 1; i <= sqrt(a); i += 1)
    if (a % i == 0) {
      v.pb(i);
      if (a / i != i)
        v.pb(a / i);
    }
  sort(all(v));
  ans = 1;
  for (i = 1; i < v.size(); i += 1)
    if (p[i] == 0) {
      ans += 1;
      for (j = i + 1; j < v.size(); j += 1)
        if (v[j] % v[i] == 0)
          p[j] = 1;
    }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mod 1000000007
#define MAX 1000000000000000
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (i = (ll)a; i < (ll)b; i++)
#define revrep(i, a, b) for (i = (ll)a; i >= (ll)b; i--)
#define ii pair<ll, ll>
#define MP make_pair
#define pb push_back
#define f first
#define se second
#define ll long long int
#define vi vector<ll>
ll modexp(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
#define rs resize
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    OST;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const ll N = 1e5 + 2;
ll i, j, a, b, ans;
vi v;
ll p[N];
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> a >> b;
  a = __gcd(a, b);
  for (i = 1; i <= sqrt(a); i += 1)
    if (a % i == 0) {
      v.pb(i);
      if (a / i != i)
        v.pb(a / i);
    }
  sort(all(v));
  ans = 1;
  for (i = 1; i < v.size(); i += 1)
    if (p[i] == 0) {
      ans += 1;
      for (j = i + 1; j < v.size(); j += 1)
        if (v[j] % v[i] == 0)
          p[j] = 1;
    }
  cout << ans;
  return 0;
}