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

#include <bits/stdc++.h>
#define pb push_back
#define vii vector<int>
#define task "ABC"
#define pll pair<ll, ll>
#define pii pair<ll, pair<ll, ll>>
#define fi first
#define se second

using ll = long long;
using ld = long double;
using ull = unsigned long long;
const ll mod = 1e9 + 7;
using namespace std;
const int N = 1e6 + 5;
ll tong, m, n, k, t, T, ntest, ans, u, v, b[N], lab[N];
string s;
ll a[N], d[N], c[N];
vector<pii> kq;
vector<pll> adj[N], adt[N];
bool check;
ll findp(ll u) { return lab[u] < 0 ? u : lab[u] = findp(lab[u]); }
void hop(ll u, ll v) {
  if (lab[u] > lab[v])
    swap(u, v);
  lab[u] += lab[v];
  lab[v] = u;
  ans = max(ans, -lab[u]);
}
inline void sol() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  check = true;
  for (int i = 1; i <= n; i++) {
    t = b[i];
    if (t == 1)
      continue;
    if (a[t]) {
      check = false;
      break;
    }
    ++a[t];
    for (int j = 2; j * j <= t; j++) {
      if (t % j == 0) {
        if (a[j]) {
          check = false;
          break;
        }
        ++a[j];
        if (j * j != t) {
          if (a[t / j]) {
            check = false;
            break;
          }
          ++a[t / j];
        }
      }
    }
    if (!check)
      break;
  }

  if (check) {
    cout << "pairwise coprime";
    return;
  }
  ans = b[1];
  for (int i = 2; i <= n; i++) {
    ans = __gcd(ans, b[i]);
    if (ans == 1) {
      cout << "setwise coprime";
      return;
    }
  }
  if (ans == 1) {
    cout << "setwise coprime";
    return;
  }
  cout << "not coprime";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  sol();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define vii vector<int>
#define task "ABC"
#define pll pair<ll, ll>
#define pii pair<ll, pair<ll, ll>>
#define fi first
#define se second

using ll = long long;
using ld = long double;
using ull = unsigned long long;
const ll mod = 1e9 + 7;
using namespace std;
const int N = 1e6 + 5;
ll tong, m, n, k, t, T, ntest, ans, u, v, b[N], lab[N];
string s;
ll a[N], d[N], c[N];
vector<pii> kq;
vector<pll> adj[N], adt[N];
bool check;
ll findp(ll u) { return lab[u] < 0 ? u : lab[u] = findp(lab[u]); }
void hop(ll u, ll v) {
  if (lab[u] > lab[v])
    swap(u, v);
  lab[u] += lab[v];
  lab[v] = u;
  ans = max(ans, -lab[u]);
}
inline void sol() {
  cin >> n;
  m = N;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }

  for (ll i = 2; i < m; i++) {
    if (d[i] == 0) {
      a[k] = i;
      for (ll j = i * i; j < m; j += i) {
        d[j] = 1;
        // a[j] = (a[j] | (1 << k));
      }
      ++k;
    }
    // cout << a[i] <<" ";
  }

  check = true;

  for (int i = 1; i <= n; i++) {
    t = b[i];
    if (t == 1)
      continue;
    for (int j = 0; j < k; j++) {
      if (a[j] > t)
        break;
      if (t % a[j] == 0) {

        if (c[j] > 0) {
          check = false;
          break;
        }
        ++c[j];
        while (t % a[j] == 0)
          t /= a[j];
      }
    }
    if (!check)
      break;
  }

  if (check) {
    cout << "pairwise coprime";
    return;
  }
  ans = b[1];
  for (int i = 2; i <= n; i++) {
    ans = __gcd(ans, b[i]);
    if (ans == 1) {
      cout << "setwise coprime";
      return;
    }
  }
  if (ans == 1) {
    cout << "setwise coprime";
    return;
  }
  cout << "not coprime";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  sol();
}