Problem p02900 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

// 最小公倍数
ll gcd(ll x, ll y) {
  ll tmp = 0;
  if (x < y) {
    tmp = x;
    x = y;
    y = tmp;
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}

// 最大公倍数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

// 階乗
ll kaijo(ll k) {
  ll sum = 1;
  for (ll i = 1; i <= k; ++i) {
    sum *= i;
    sum %= 1000000000 + 7;
  }
  return sum;
}

// for(int i = ; i < ; i++){}

ll lmax(ll s, ll t) {
  if (s > t) {
    return s;
  } else {
    return t;
  }
}

ll lmin(ll s, ll t) {
  if (s < t) {
    return s;
  } else {
    return t;
  }
}
// ここから開始

int main() {
  ll a, b;
  cin >> a >> b;
  if (a > b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  // 素因数分解
  vector<ll> v;
  vector<bool> prime_count(10000000000, false);
  v.push_back(1);
  ll n = a;
  for (ll i = 2; i < sqrt(n) + 2; i++) {
    if (a % i != 0) {
      continue;
    } else {
      while (a % i == 0) {
        if (!prime_count[i]) {
          prime_count[i] = true;
          v.push_back(i);
          a /= i;
        } else {
          a /= i;
        }
      }
    }

    if (a == 1)
      break;
  }
  if (a > 1)
    v.push_back(a);
  // for(int i = 0; i<v.size(); i++) cout<<v[i]<<endl;
  ll cnt = 0;
  for (int i = 0; i < v.size(); i++) {
    if (b % v[i] == 0) {
      cnt++;
    }
  }
  // debug

  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

// 最小公倍数
ll gcd(ll x, ll y) {
  ll tmp = 0;
  if (x < y) {
    tmp = x;
    x = y;
    y = tmp;
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}

// 最大公倍数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

// 階乗
ll kaijo(ll k) {
  ll sum = 1;
  for (ll i = 1; i <= k; ++i) {
    sum *= i;
    sum %= 1000000000 + 7;
  }
  return sum;
}

// for(int i = ; i < ; i++){}

ll lmax(ll s, ll t) {
  if (s > t) {
    return s;
  } else {
    return t;
  }
}

ll lmin(ll s, ll t) {
  if (s < t) {
    return s;
  } else {
    return t;
  }
}
// ここから開始

int main() {
  ll a, b;
  cin >> a >> b;
  if (a > b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  // 素因数分解
  vector<ll> v;
  vector<bool> prime_count(100000000, false);
  v.push_back(1);
  ll n = a;
  for (ll i = 2; i < sqrt(n) + 2; i++) {
    if (a % i != 0) {
      continue;
    } else {
      while (a % i == 0) {
        if (!prime_count[i]) {
          prime_count[i] = true;
          v.push_back(i);
          a /= i;
        } else {
          a /= i;
        }
      }
    }

    if (a == 1)
      break;
  }
  if (a > 1)
    v.push_back(a);
  // for(int i = 0; i<v.size(); i++) cout<<v[i]<<endl;
  ll cnt = 0;
  for (int i = 0; i < v.size(); i++) {
    if (b % v[i] == 0) {
      cnt++;
    }
  }
  // debug

  cout << cnt << endl;
  return 0;
}