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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define INF (int)1e9 + 4
#define INFL (ll)1e18 + 4
#define md (int)1e9 + 7
#define mx 10000000 // use fixed<<setprecion
#define mk make_pair
#define pi pair<int, int>
#define ss second
#define ff first
#define pb emplace_back
#define onell(x) __builtin_popcountll(x)
#define one(x) __builtin_popcount(x)
#define maxi(a) *max_element(a.begin(), a.end());
#define mini(a) *min_element(a.begin(), a.end());
#define all(s) s.begin(), s.end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(x, a) for (auto &x : a)
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) ((long long)x.size())
#define mez(s) memset(s, 0, sizeof s)
#define mex(s) memset(s, -1, sizeof s)
#define metr(s) memset(s, true, sizeof s)
typedef vector<int> vi;
typedef vector<ll> vl;
typedef map<int, int> mp;
typedef queue<int> que;
typedef vector<pi> vp;
const int mxn = 1e6 + 67;
int gh[mxn];
void solve() {
  int n = 0, x = 0, y = 0, z = 0;
  cin >> n;
  vi a(n);
  rep(i, 0, n) {
    cin >> a[i];
    gh[a[i]]++;
  }
  bool all = 0, pair = 0;
  rep(i, 2, mxn) {
    int c = 0;
    for (int j = i; j < mxn; j += i) {
      c += gh[j];
    }
    if (c == n)
      all = true;
    if (c > 1)
      pair = true;
  }
  if (!pair)
    cout << "pairwise coprime";
  else if (!all)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define INF (int)1e9 + 4
#define INFL (ll)1e18 + 4
#define md (int)1e9 + 7
#define mx 10000000 // use fixed<<setprecion
#define mk make_pair
#define pi pair<int, int>
#define ss second
#define ff first
#define pb emplace_back
#define onell(x) __builtin_popcountll(x)
#define one(x) __builtin_popcount(x)
#define maxi(a) *max_element(a.begin(), a.end());
#define mini(a) *min_element(a.begin(), a.end());
#define all(s) s.begin(), s.end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(x, a) for (auto &x : a)
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) ((long long)x.size())
#define mez(s) memset(s, 0, sizeof s)
#define mex(s) memset(s, -1, sizeof s)
#define metr(s) memset(s, true, sizeof s)
typedef vector<int> vi;
typedef vector<ll> vl;
typedef map<int, int> mp;
typedef queue<int> que;
typedef vector<pi> vp;
const int mxn = 1e7 + 6;
bool vis[mxn];
void sieve() {
  metr(vis);
  for (int i = 2; i * i <= mxn; i++) {
    if (vis[i])
      for (int j = i * i; j <= mxn; j += i) {
        vis[j] = false;
      }
  }
}
void solve() {
  int n = 0, x = 0, y = 0, z = 0, m = 0, k = 0;
  ll ans = 0;
  sieve();
  cin >> n;
  vi a(n);
  rep(i, 0, n) cin >> a[i];
  x = maxi(a);
  bool c = 1;
  for (int i = 2; i <= x; i++) {
    int d = 0;
    if (vis[i]) {
      rep(j, 0, n) {
        if (a[j] % i == 0) {
          d++;
        }
      }
      if (d >= 2) {
        c = 0;
        break;
      }
    }
  }
  if (c) {
    cout << "pairwise coprime";
    return;
  }
  rep(i, 0, n) { y = __gcd(y, a[i]); }
  if (y == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}