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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define rrep(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr ll INF = (ll)1e9;
constexpr ll mod = (ll)1e9 + 7;
constexpr double PI = 3.1415926535897932;
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;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

ll gcd(ll x, ll y) { return (y ? gcd(y, x % y) : x); }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

const int A = 1000010;
int main() {
  init();

  int N;
  cin >> N;
  vector<int> vec(N);
  vector<int> c(A, 0);
  rep(i, 0, N) {
    cin >> vec[i];
    c[vec[i]]++;
  }
  bool ok = true;
  rep(i, 2, A) {
    int cnt = 0;
    for (int j = i; j < A; j += i) {
      cnt += c[j];
    }
    if (cnt >= 2)
      ok = false;
  }
  ll g = vec[0];
  rep(i, 0, N) g = gcd(g, vec[i]);
  if (ok) {
    puts("pairwise coprime");
    return 0;
  } else if (g == 1) {
    puts("setwise coprime");
  } else {
    puts("not coprime");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define rrep(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr ll INF = (ll)1e9;
constexpr ll mod = (ll)1e9 + 7;
constexpr double PI = 3.1415926535897932;
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;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

ll gcd(ll x, ll y) { return (y ? gcd(y, x % y) : x); }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

vector<int> prime(1000010);

int main() {
  init();

  rep(i, 0, prime.size()) { prime[i] = i; }
  prime[0] = 0;
  prime[1] = 0;

  for (int i = 2; i * i < prime.size(); ++i) {
    if (prime[i] == i) {
      for (int j = i; j < prime.size(); j += i) {
        prime[j] = i;
      }
    }
  }

  int N;
  cin >> N;
  vector<int> vec(N);
  rep(i, 0, N) cin >> vec[i];
  unordered_set<int> st;
  ll g = vec[0];
  bool ok = true;
  rep(i, 0, N) {
    g = gcd(g, vec[i]);

    auto st2 = st;
    int v = vec[i];
    while (v > 1) {
      if (st2.count(prime[v])) {
        ok = false;
        break;
      } else if (!st.count(prime[v]))
        st.insert(prime[v]);
      v /= prime[v];
    }
  }
  if (ok) {
    puts("pairwise coprime");
    return 0;
  } else if (g == 1) {
    puts("setwise coprime");
  } else {
    puts("not coprime");
  }

  return 0;
}