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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

/*
ID: febrian3
LANG: C++11
PROB: task
*/

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();
#define fi first
#define se second
#define pf push_front
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define debug(x) cerr << #x << ": " << (x) << '\\n'
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll nax = 0;

int main() {
  /*freopen("filename.in","r",stdin);
  freopen("filename.out","w",stdout);*/
  IOS bool pair = 1, sw = 1;
  ll n;
  cin >> n;
  vector<ll> arr(n + 1), cnt(1e6 + 5, 0);
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    cnt[arr[i]]++;
  }
  for (int i = 2; i <= 1e6; ++i) {
    ll ans = 0;
    for (int j = i; j <= 1e6; j += i) {
      ans += cnt[j];
    }
    if (ans > 1)
      pair = 0;
    if (ans >= n)
      sw = 0;
    if (!pair && !sw)
      break;
  }
  if (pair) {
    cout << "pairwise coprime" << '\\n';
  } else if (sw) {
    cout << "setwise coprime" << '\\n';
  } else
    cout << "not coprime" << '\\n';
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

/*
ID: febrian3
LANG: C++11
PROB: task
*/

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();
#define fi first
#define se second
#define pf push_front
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define debug(x) cerr << #x << ": " << (x) << '\\n'
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll nax = 0;
ll n;
vector<ll> cnt(1000005, 0);
bool setwise = 0;

void solve(ll x) {
  ll batas = sqrt(x);
  for (int i = 1; i <= batas; ++i) {
    if (x % i == 0) {
      if (x / i == i) {
        cnt[i]++;
        if (cnt[i] == n && i != 1) {
          cout << "not coprime" << '\\n';
          exit(0);
        }
        if (cnt[i] > 1 && i != 1)
          setwise = 1;
      } else {
        cnt[i]++;
        cnt[x / i]++;
        if (cnt[i] == n && i != 1) {
          cout << "not coprime" << '\\n';
          exit(0);
        }
        if (cnt[i] > 1 && i != 1)
          setwise = 1;
        if (cnt[x / i] == n && x / i != 1) {
          cout << "not coprime" << '\\n';
          exit(0);
        }
        if (cnt[x / i] > 1 && x / i != 1)
          setwise = 1;
      }
    }
  }
}

int main() {
  /*freopen("filename.in","r",stdin);
  freopen("filename.out","w",stdout);*/
  IOS cin >> n;
  vector<ll> arr(n);
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; ++i) {
    solve(arr[i]);
  }
  if (setwise)
    cout << "setwise coprime" << '\\n';
  else
    cout << "pairwise coprime" << '\\n';
}