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

#include <bits/stdc++.h>
#define endl '\\n'
#define FAST_IO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define IT set<node>::iterator
#define mem(a) memset(a, 0, sizeof(a))
typedef long long LL;
typedef long long ll;
using namespace std;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
bool isprime[N];
int prime[N], tot;
void init() {
  for (int i = 2; i <= 1e6; i++) {
    if (!isprime[i]) {
      prime[++tot] = i;
      for (int j = 2 * i; j <= 1e6; j += i) {
        isprime[j] = true;
      }
    }
  }
}
ll vis[N], a[N];
int main() {
  init();
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int n;
  cin >> n;
  isprime[1] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll d = __gcd(a[1], a[2]);
  for (int i = 3; i <= n; i++) {
    d = __gcd(d, a[i]);
  }
  if (d > 1) {
    cout << "not coprime\\n";
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (!isprime[a[i]]) {
      vis[a[i]]++;
      continue;
    }
    int len = sqrt(a[i]);
    for (int j = 1; j <= tot && prime[j] <= len; j++) {
      if (a[i] % prime[j] == 0) {
        vis[prime[j]]++;
        if (!isprime[a[i] / prime[j]] && a[i] / prime[j] != prime[j] &&
            prime[j] < a[i] / prime[j]) {
          vis[a[i] / prime[j]]++;
        }
      }
    }
  }
  int flag1 = 0, flag2 = 0;
  for (int i = 1; i <= tot; i++) {
    if (vis[prime[i]] > 1) {
      flag1 = 1;
    }
    if (vis[prime[i]] == n) {
      flag2 = 1;
    }
  }
  if (!flag1) {
    cout << "pairwise coprime\\n";
  } else if (flag1 && !flag2) {
    cout << "setwise coprime\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define FAST_IO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define IT set<node>::iterator
#define mem(a) memset(a, 0, sizeof(a))
typedef long long LL;
typedef long long ll;
using namespace std;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
bool isprime[N];
int prime[N], tot;
void init() {
  for (int i = 2; i <= 1e6; i++) {
    if (!isprime[i]) {
      prime[++tot] = i;
      for (int j = 2 * i; j <= 1e6; j += i) {
        isprime[j] = true;
      }
    }
  }
}
int vis[N], a[N];
int main() {
  init();
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (!isprime[a[i]]) {
      vis[a[i]]++;
      continue;
    }
    for (int j = 1; j <= tot && prime[j] <= a[i]; j++) {
      if (a[i] % prime[j] == 0) {
        vis[prime[j]]++;
        while (a[i] % prime[j] == 0) {
          a[i] /= prime[j];
        }
      }
    }
  }
  int flag1 = 0, flag2 = 0;
  for (int i = 1; i <= tot; i++) {
    // cout << vis[prime[i]] << ' ' << prime[i] << "\\n";
    if (vis[prime[i]] > 1) {
      flag1 = 1;
    }
    if (vis[prime[i]] == n) {
      flag2 = 1;
    }
  }
  if (!flag1) {
    cout << "pairwise coprime\\n";
  } else if (flag1 && !flag2) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }

  return 0;
}