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

// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T> inline bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
struct IOSET {
  IOSET() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  }
} ioset;

// Greatest Common Divisor and Least Common Multiple
ll GCD(ll x, ll y) {
  if (!y)
    return x;
  return GCD(y, x % y);
}

const int MAX = 1000010;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> p(MAX + 1, 1);
  p[0] = p[1] = 0;
  rep(i, 2, MAX + 1) {
    if (not p[i])
      continue;
    for (int j = i * 2; j <= MAX; j += i)
      p[j] = 0;
  }
  vector<int> cnt(MAX + 1, 0);
  rep(i, n)++ cnt[a[i]];

  bool pairwise = true, setwise = true;
  rep(i, 2, MAX + 1) {
    if (not p[i])
      continue;
    int c = 0;
    for (int j = i; j <= MAX; j += i)
      c += cnt[j];
    if (c >= 2)
      pairwise = false;
    if (c == n)
      setwise = false;
  }
  if (pairwise) {
    cout << "pairwise coprime\\n";
    return 0;
  }
  if (setwise) {
    cout << "setwise coprime\\n";
    return 0;
  }
  cout << "not coprime\\n";
  return 0;
}


Error: TLE

Fixed submission:

// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T> inline bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
struct IOSET {
  IOSET() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  }
} ioset;

// Greatest Common Divisor and Least Common Multiple
ll GCD(ll x, ll y) {
  if (!y)
    return x;
  return GCD(y, x % y);
}

const int MAX = 1000010;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> p(MAX + 1, 1);
  p[0] = p[1] = 0;
  rep(i, 2, MAX + 1) {
    if (not p[i])
      continue;
    for (int j = i * 2; j <= MAX; j += i)
      p[j] = 0;
  }

  bool pairwise = true, setwise = true;
  rep(i, 2, MAX + 1) {
    if (not p[i])
      continue;
    int cnt = 0;
    rep(j, n) {
      if (a[j] % i == 0)
        ++cnt;
    }
    if (cnt >= 2)
      pairwise = false;
    if (cnt == n)
      setwise = false;
  }
  if (pairwise) {
    cout << "pairwise coprime\\n";
    return 0;
  }
  if (setwise) {
    cout << "setwise coprime\\n";
    return 0;
  }
  cout << "not coprime\\n";
  return 0;
}