Problem p02900 (C++)

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

using namespace std;

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(const char &ch) { return string(1, ch); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define pr(...) 42
#endif

double tick() {
  static clock_t oldtick;
  clock_t newtick = clock();
  double diff = 1.0 * (newtick - oldtick) / CLOCKS_PER_SEC;
  oldtick = newtick;
  return diff;
}
typedef long long ll;

vector<ll> factorise(ll val) {
  vector<ll> fac;
  for (ll n = 1; n * n <= val; n++) {
    if (val % n == 0) {
      if (val / n == n)
        fac.emplace_back(n);
      else {
        fac.emplace_back(n);
        fac.emplace_back(val / n);
      }
    }
  }

  return fac;
}

ll gcd(ll a, ll b) {
  while (b > 0) {
    ll temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(10);
  cout << fixed;
#ifdef LOCAL
  freopen("d_commmon_divisors.in", "rt", stdin);
#endif

  ll a, b;
  cin >> a >> b;
  vector<ll> faca = factorise(a);
  vector<ll> facb = factorise(b);
  int an = faca.size();
  int bn = facb.size();

  map<ll, int> mp;
  for (int i = 0; i < an; i++) {
    mp[faca[i]]++;
  }
  for (int i = 0; i < bn; i++) {
    mp[facb[i]]++;
  }
  vector<ll> arr;
  for (auto pr : mp) {
    if (pr.second == 2)
      arr.emplace_back(pr.first);
  }

  // pr(arr);
  int n = arr.size();
  vector<vector<ll>> vec(n);
  for (int i = 0; i < n; i++) {
    vec[i].emplace_back(arr[i]);
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      else {
        bool found = false;
        for (ll val : vec[j]) {
          if (gcd(val, arr[i]) != 1LL) {
            found = true;
            break;
          }
        }
        if (!found)
          vec[j].emplace_back(arr[i]);
      }
    }
  }

  // pr(vec);

  int ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    int size = vec[i].size();
    ans = max(ans, size);
  }
  cout << ans << endl;

#ifdef LOCAL
  // fprintf(stderr, "Time elapsed: %.12lf secs...\n", tick());
#endif
  return 0;
}


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

using namespace std;

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(const char &ch) { return string(1, ch); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define pr(...) 42
#endif

double tick() {
  static clock_t oldtick;
  clock_t newtick = clock();
  double diff = 1.0 * (newtick - oldtick) / CLOCKS_PER_SEC;
  oldtick = newtick;
  return diff;
}
typedef long long ll;

vector<ll> factorise(ll val) {
  vector<ll> fac;
  for (ll n = 1; n * n <= val; n++) {
    if (val % n == 0) {
      if (val / n == n)
        fac.emplace_back(n);
      else {
        fac.emplace_back(n);
        fac.emplace_back(val / n);
      }
    }
  }

  return fac;
}

ll gcd(ll a, ll b) {
  while (b > 0) {
    ll temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(10);
  cout << fixed;
#ifdef LOCAL
  freopen("d_commmon_divisors.in", "rt", stdin);
#endif

  ll a, b;
  cin >> a >> b;
  vector<ll> faca = factorise(a);
  vector<ll> facb = factorise(b);
  int an = faca.size();
  int bn = facb.size();

  map<ll, int> mp;
  for (int i = 0; i < an; i++) {
    mp[faca[i]]++;
  }
  for (int i = 0; i < bn; i++) {
    mp[facb[i]]++;
  }
  vector<ll> arr;
  for (auto pr : mp) {
    if (pr.second == 2)
      arr.emplace_back(pr.first);
  }

  // pr(arr);
  int n = arr.size();
  vector<vector<ll>> vec(n);
  for (int i = 0; i < n; i++) {
    vec[i].emplace_back(arr[i]);
  }

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i == j)
        continue;
      else {
        bool found = false;
        for (ll val : vec[j]) {
          if (gcd(val, arr[i]) != 1LL) {
            found = true;
            break;
          }
        }
        if (!found)
          vec[j].emplace_back(arr[i]);
      }
    }
  }

  // pr(vec);

  int ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    int size = vec[i].size();
    ans = max(ans, size);
  }
  cout << ans << endl;

#ifdef LOCAL
  // fprintf(stderr, "Time elapsed: %.12lf secs...\n", tick());
#endif
  return 0;
}