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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n, a)                                                              \\
  cin >> n;                                                                    \\
  for (int i = 1; i <= n; i++)                                                 \\
  cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev coyhhhhhhyoc
#define next sdNNNmNNNNNNNmds
#define y0 hNNNNy_yNNNNNN_sNh
#define y1 mNNNNNdtdNNNNNNtsNNm
#define yn mNNNNNNNNy___smNNNms
#define tm oooooosyysooooot
#define read tyhyt
#define rank ytmNmo
#define index yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i, a, b) for (int i = (b); i >= (a); i--)
#define rall(x, a) for (auto x : a)
#define MOD 1000000007
#define endl "\\n"
typedef long long ll;
using namespace std;

const int N = 1111111;
int a[N], cnt[N], cntd[N];
int n;

int solve() {
  ina(n, a);
  rep(i, 1, n) { cnt[a[i]]++; }
  bool ok = true;
  rep(x, 2, N - 1) {
    int cur = 0;
    for (int y = x; y < N; y += x) {
      cur += cnt[y];
    }
    if (cur > 1) {
      ok = false;
      break;
    }
  }
  if (ok) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  int g = a[1];
  rep(i, 2, n) { g = __gcd(g, a[i]); }
  if (g == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  cout << "not coprime" << endl;
  return 0;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n, a)                                                              \\
  cin >> n;                                                                    \\
  for (int i = 1; i <= n; i++)                                                 \\
  cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev coyhhhhhhyoc
#define next sdNNNmNNNNNNNmds
#define y0 hNNNNy_yNNNNNN_sNh
#define y1 mNNNNNdtdNNNNNNtsNNm
#define yn mNNNNNNNNy___smNNNms
#define tm oooooosyysooooot
#define read tyhyt
#define rank ytmNmo
#define index yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i, a, b) for (int i = (b); i >= (a); i--)
#define rall(x, a) for (auto x : a)
#define MOD 1000000007
#define endl "\\n"
typedef long long ll;
using namespace std;

const int N = 1111111;
vector<int> di[N];
int a[N], cnt[N], cntd[N];
int n;

int solve() {
  ina(n, a);
  rep(i, 1, n) { cnt[a[i]]++; }
  rep(x, 2, N - 1) {
    for (int y = x; y < N; y += x) {
      di[y].pb(x);
    }
  }
  bool ok = true;
  rep(i, 1, n) {
    rall(d, di[a[i]]) {
      cntd[d]++;
      if (cntd[d] > 1) {
        ok = false;
        break;
      }
    }
  }
  if (ok) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  int g = a[1];
  rep(i, 2, n) { g = __gcd(g, a[i]); }
  if (g == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  cout << "not coprime" << endl;
  return 0;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}