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

#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define lli long long int
#define rep(i, n, z) for (int i = z; i < n; i++)
#define rrep(i, z) for (int i = z; i >= 0; i--)
#define nl cout << endl
#define vi vector<int>
#define vlli vector<long long int>
#define umap unordered_map
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define ipair pair<int, int>
#define llipair pair<lli, lli>
#define pq priority_queue
#define displaymatrix(a, m, n)                                                 \\
  for (int i = 0; i < m; i++) {                                                \\
    for (int j = 0; j < n; j++)                                                \\
      cout << a[i][j] << " ";                                                  \\
    cout << endl;                                                              \\
  }
#define printarray(a, n)                                                       \\
  for (int i = 0; i < n; i++) {                                                \\
    cout << a[i] << " ";                                                       \\
  }                                                                            \\
  nl;
#define vinput(a, n)                                                           \\
  vlli a(n);                                                                   \\
  rep(i, n, 0) cin >> a[i]
#define ainput(a, n) rep(i, n, 0) cin >> a[i]
#define SO(a) sort(a.begin(), a.end())
#define all(x) (x).begin(), (x).end()
#define SOP(a, comp) sort(a.begin(), a.end(), comp)
#define inf INT_MAX
#define endl '\\n'

int main() {
  fastio;
  int n;
  cin >> n;
  vinput(a, n);

  lli gcd = 0;

  rep(i, n, 0) gcd = __gcd(gcd, a[i]);

  if (gcd != 1)
    return cout << "not coprime" << endl, 0;

  vi ps(1e6 + 5, true), pc(1e6 + 5, 0);
  ps[0] = ps[1] = false;
  vi primes;

  rep(i, ps.size(), 2) {
    if (!ps[i])
      continue;
    primes.pb(i);
    for (int j = i * i; j < ps.size(); j += i)
      ps[i] = false;
  }

  for (int i = 0; i < n; i++) {
    int val = sqrt(a[i]);

    for (auto x : primes) {
      if (val < x)
        break;
      if (a[i] % x)
        continue;
      // cout<<x<<" ";
      if (pc[x])
        return cout << "setwise coprime" << endl, 0;
      pc[x]++;

      while (a[i] % x == 0)
        a[i] /= x;
    }

    if (a[i] > 1) {
      if (pc[a[i]])
        return cout << "setwise coprime" << endl, 0;
      pc[a[i]]++;
    }
  }

  cout << "pairwise coprime" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define lli long long int
#define rep(i, n, z) for (int i = z; i < n; i++)
#define rrep(i, z) for (int i = z; i >= 0; i--)
#define nl cout << endl
#define vi vector<int>
#define vlli vector<long long int>
#define umap unordered_map
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define ipair pair<int, int>
#define llipair pair<lli, lli>
#define pq priority_queue
#define displaymatrix(a, m, n)                                                 \\
  for (int i = 0; i < m; i++) {                                                \\
    for (int j = 0; j < n; j++)                                                \\
      cout << a[i][j] << " ";                                                  \\
    cout << endl;                                                              \\
  }
#define printarray(a, n)                                                       \\
  for (int i = 0; i < n; i++) {                                                \\
    cout << a[i] << " ";                                                       \\
  }                                                                            \\
  nl;
#define vinput(a, n)                                                           \\
  vlli a(n);                                                                   \\
  rep(i, n, 0) cin >> a[i]
#define ainput(a, n) rep(i, n, 0) cin >> a[i]
#define SO(a) sort(a.begin(), a.end())
#define all(x) (x).begin(), (x).end()
#define SOP(a, comp) sort(a.begin(), a.end(), comp)
#define inf INT_MAX
#define endl '\\n'

int main() {
  fastio;
  int n;
  cin >> n;
  vinput(a, n);

  lli gcd = 0;

  rep(i, n, 0) gcd = __gcd(gcd, a[i]);

  if (gcd != 1)
    return cout << "not coprime" << endl, 0;

  vi ps(1e6 + 5, true), pc(1e6 + 5, 0);
  ps[0] = ps[1] = false;
  vi primes;

  rep(i, ps.size(), 2) {
    if (!ps[i])
      continue;
    primes.pb(i);
    for (int j = i * i; j < ps.size(); j += i)
      ps[i] = false;
  }

  for (int i = 0; i < n; i++) {
    for (auto x : primes) {
      if (a[i] % x)
        continue;
      if (pc[x])
        return cout << "setwise coprime" << endl, 0;
      pc[x]++;

      while (a[i] % x == 0)
        a[i] /= x;
    }
  }

  cout << "pairwise coprime" << endl;
}