Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1'000'000'007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

set<int> cand;
map<int, int> mp;

vector<pair<int, int>> factorization(int num) {
  map<int, int> mp;

  int tmp = num;
  for (int i = 2; i * i <= num; i++) {
    while (tmp % i == 0) {
      mp[i]++;
      tmp /= i;
    }
  }
  if (tmp != 1) {
    mp[tmp]++;
  }
  vector<pair<int, int>> ret;
  for (auto m : mp) {
    ret.push_back(make_pair(m.first, m.second));
  }
  return ret;
}

void f(int a) {
  vector<int> el;
  for (auto c : cand) {
    if (a % c == 0) {
      ++mp[c];
    } else {
      el.push_back(c);
    }
  }
  for (int e : el) {
    cand.erase(e);
  }
  return;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  bool flag = false;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (!flag) {
      auto fac = factorization(a);
      for (auto f : fac) {
        ++mp[f.first];
        if (mp[f.first] > 1) {
          flag = true;
          continue;
        }
        cand.insert(f.first);
      }
    } else {
      f(a);
    }
  }
  bool pw_flag = true;
  for (auto m : mp) {
    if (m.second == n) {
      cout << "not coprime" << endl;
      return 0;
    }
    if (m.second > 1) {
      pw_flag = false;
    }
  }
  if (pw_flag) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1'000'000'007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

vector<pair<int, int>> factorization(int num) {
  map<int, int> mp;

  int tmp = num;
  for (int i = 2; i * i <= num; i++) {
    while (tmp % i == 0) {
      mp[i]++;
      tmp /= i;
    }
  }
  if (tmp != 1) {
    mp[tmp]++;
  }
  vector<pair<int, int>> ret;
  for (auto m : mp) {
    ret.push_back(make_pair(m.first, m.second));
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    auto fac = factorization(a);
    for (auto f : fac) {
      ++mp[f.first];
    }
  }
  bool pw_flag = true;
  for (auto m : mp) {
    if (m.second == n) {
      cout << "not coprime" << endl;
      return 0;
    }
    if (m.second > 1) {
      pw_flag = false;
    }
  }
  if (pw_flag) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  return 0;
}