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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[1000006];
int vis[1000006];
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int gc = 0;
  bool chk = 0;
  int cnt1 = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    gc = __gcd(gc, x);
    cnt[x]++;
    if (cnt[x] > 1 && x != 1) {
      chk = 1;
    }
    if (x % 2 == 0)
      cnt1++;
    if (cnt1 > 1)
      chk = 1;
  }
  if (chk == 0) {
    for (int i = 2; i <= 1e6; i += 2) {
      vis[i] = 1;
    }
    for (int i = 3; i <= 1e6; i += 2) {
      int ase = 0;
      for (int j = i; j <= 1e6; j += i) {
        ase += cnt[j];
        vis[j] = 1;
      }
      if (ase > 1) {
        chk = 1;
        break;
      }
    }
  }
  if (gc != 1) {
    cout << "not coprime\\n";
  } else {
    if (chk == 0) {
      cout << "pairwise coprime\\n";
    } else {
      cout << "setwise coprime\\n";
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define BOOST                                                                  \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define all(v) v.begin(), v.end()
#define sl(a) a.size()
#define mod 1000000007
#define lcm(a, b) (a * b) / __gcd(a, b)
#define MAX1 100006
#define f first
#define s second
#define pi 2 * acos(0.0)
bool isprime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
void solve() {
  ll n;
  cin >> n;
  set<ll> se, prime;
  ll gc;
  rep(i, 0, n) {
    ll x;
    cin >> x;
    if (i == 0)
      gc = x;
    if (!isprime(x))
      se.insert(x);
    else
      prime.insert(x);
    gc = __gcd(gc, x);
  }
  if (gc != 1)
    cout << "not coprime";
  else {
    if (sl(se) < 1) {
      cout << "pairwise coprime";
      return;
    }
    if (sl(se) == 1) {
      ll j = *se.begin();
      for (auto i : prime) {
        if (__gcd(i, j) != 1) {
          cout << "setwise coprime";
          return;
        }
      }
      cout << "pairwise coprime";
      return;
    }
    for (auto i : prime) {
      for (auto j : se) {
        if (__gcd(i, j) != 1) {
          cout << "setwise coprime";
          return;
        }
      }
    }
    for (auto i : se) {
      for (auto j : se) {
        if (i != j and __gcd(i, j) != 1) {
          cout << "setwise coprime";
          return;
        }
      }
    }
    cout << "pairwise coprime";
  }
}
int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for getting error from error.txt
  freopen("error.txt", "w", stderr);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  BOOST
  int T = 1; // cin>>T;
  while (T--)
    solve();
  cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
       << "\\n";
  cerr << "My CLOCKS_PER_SEC= " << CLOCKS_PER_SEC << "\\n";
  return 0;
}