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

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define rep(i, beg, end) for (int i = (beg); i < (end); i++)
#define rrep(i, beg, end) for (int i = (beg); i >= (end); i--)
#define sp(i) setprecision(i) << fixed
typedef long long ll;
typedef pair<ll, ll> P;
typedef unsigned long long ull;
const int INF = 2e9;
const int mod = 1e9 + 7;
const int N = 1000010;
ull p_hash(ull x, ull y) { return (x + y) * (x + y + 1) / 2 + y; } // NxN->N
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

bool notPirme[N + 1];
int primes[N + 1];
int cnt;
int a[N];
int num[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  for (int i = 2; i <= N; i++) {
    if (!notPirme[i])
      primes[cnt++] = i;

    for (int j = 0; primes[j] <= N / i; j++) {
      notPirme[primes[j] * i] = true;
      if (i % primes[j] == 0)
        break;
    }
  }

  int n;
  cin >> n;
  rep(i, 0, n) {
    cin >> a[i];
    num[a[i]]++;
  }
  bool pw = true;
  for (int i = 0; i < cnt; i++) {
    int cnt = 0;
    for (int j = primes[i]; j < N; j += primes[i]) {
      cnt += num[j];
    }
    if (cnt > 1)
      pw = false;
  }

  if (pw) {
    cout << "pairwise coprime" << endl;
  } else {
    int g = 0;
    rep(i, 0, n) g = gcd(g, a[i]);

    if (g == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define rep(i, beg, end) for (int i = (beg); i < (end); i++)
#define rrep(i, beg, end) for (int i = (beg); i >= (end); i--)
#define sp(i) setprecision(i) << fixed
typedef long long ll;
typedef pair<ll, ll> P;
typedef unsigned long long ull;
const int INF = 2e9;
const int mod = 1e9 + 7;
const int N = 1000010;
ull p_hash(ull x, ull y) { return (x + y) * (x + y + 1) / 2 + y; } // NxN->N
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

bool notPirme[N + 1];
int primes[N + 1];
int cnt;
int a[N];
int mk[N];
int check[N];
int num[N];
int ma = -INF;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  for (int i = 2; i <= N; i++) {
    if (!notPirme[i])
      primes[cnt++] = i;

    for (int j = 0; primes[j] <= N / i; j++) {
      notPirme[primes[j] * i] = true;
      if (i % primes[j] == 0)
        break;
    }
  }

  int n;
  scanf("%d", &n);
  rep(i, 0, n) {
    scanf("%d", &a[i]);
    ma = max(ma, a[i]);
    num[a[i]]++;
  }

  rep(i, 0, n) {
    if (check[a[i]])
      continue;
    else
      check[a[i]] = true;

    int t = a[i];
    rep(j, 0, N) {
      if (a[i] % primes[j] == 0)
        mk[primes[j]] += num[t];
      while (a[i] % primes[j] == 0) {
        a[i] /= primes[j];
      }
      if (a[i] == 1)
        break;
    }
  }
  int flag = 0;
  rep(i, 0, cnt) {
    if (primes[i] > ma)
      break;
    if (mk[primes[i]] >= 2)
      flag = 1;

    if (mk[primes[i]] == n) {
      flag = 2;
      break;
    }
  }

  if (!flag)
    cout << "pairwise coprime" << endl;
  else if (flag == 2)
    cout << "not coprime" << endl;
  else
    cout << "setwise coprime" << endl;

  return 0;
}