Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

const int INF = 1 << 28;
const long long MOD = 1000000007;
using namespace std;
using Graph = vector<vector<int>>;

template <class T> bool chmin(T &a, T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> using P = pair<T, U>;

vector<int> seen(50, -1);
int counter_for_dfs = 0;
void dfs(Graph &G, int v) {
  seen[v] = counter_for_dfs;
  counter_for_dfs++;
  for (auto v_next : G[v]) {
    if (seen[v_next] >= 0)
      continue;

    dfs(G, v_next);
  }
}

void bfs(Graph &G) {
  vector<int> dist(G.size(), -1);
  queue<int> que;

  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (int g : G[v]) {
      if (dist[g] != -1)
        continue;
      dist[g] = dist[v] + 1;
      que.push(g);
    }
  }
  for (auto i : dist) {
    cout << i << " ";
  }
}

int counter[200005] = {};
void bfs2(Graph &G, int p, int x) {
  vector<int> dist(G.size(), -1);
  queue<int> que;

  dist[p] = 0;
  que.push(p);
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (int g : G[v]) {
      if (dist[g] != -1)
        continue;
      dist[g] = dist[v] + 1;
      que.push(g);
      counter[g] += x;
    }
  }
}
long long int gcd(long long int a, long long int b) {
  long long int c = std::min(a, b);
  a = std::max(a, b);
  b = c;
  while (true) {
    c = a % b;
    a = b;
    b = c;
    if (c == 0)
      break;
  }
  return a;
}
int main() {
  long long int a, b;
  std::cin >> a >> b;
  long long int c = gcd(a, b);

  long long int counter = 1;

  for (long long int i = 2; i < c; i++) {
    if (c % i == 0) {
      counter++;
      while (c % i == 0)
        c /= i;
    }
  }
  if (c != 1)
    counter++;

  std::cout << counter;

  return 0;
}

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

const int INF = 1 << 28;
const long long MOD = 1000000007;
using namespace std;
using Graph = vector<vector<int>>;

template <class T> bool chmin(T &a, T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> using P = pair<T, U>;

vector<int> seen(50, -1);
int counter_for_dfs = 0;
void dfs(Graph &G, int v) {
  seen[v] = counter_for_dfs;
  counter_for_dfs++;
  for (auto v_next : G[v]) {
    if (seen[v_next] >= 0)
      continue;

    dfs(G, v_next);
  }
}

void bfs(Graph &G) {
  vector<int> dist(G.size(), -1);
  queue<int> que;

  dist[0] = 0;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (int g : G[v]) {
      if (dist[g] != -1)
        continue;
      dist[g] = dist[v] + 1;
      que.push(g);
    }
  }
  for (auto i : dist) {
    cout << i << " ";
  }
}

int counter[200005] = {};
void bfs2(Graph &G, int p, int x) {
  vector<int> dist(G.size(), -1);
  queue<int> que;

  dist[p] = 0;
  que.push(p);
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (int g : G[v]) {
      if (dist[g] != -1)
        continue;
      dist[g] = dist[v] + 1;
      que.push(g);
      counter[g] += x;
    }
  }
}
long long int gcd(long long int a, long long int b) {
  long long int c = std::min(a, b);
  a = std::max(a, b);
  b = c;
  while (true) {
    c = a % b;
    a = b;
    b = c;
    if (c == 0)
      break;
  }
  return a;
}
int main() {
  long long int a, b;
  std::cin >> a >> b;
  long long int c = gcd(a, b);

  long long int counter = 1;

  for (long long int i = 2; i * i < c; i++) {
    if (c % i == 0) {
      counter++;
      while (c % i == 0)
        c /= i;
    }
  }
  if (c != 1)
    counter++;

  std::cout << counter;

  return 0;
}