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

#include <bits/stdc++.h>

using namespace std;

#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
// #define int ll
#define vi vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define pb push_back
#define B begin()
#define E end()
#define sz(A) (ll)(A).size()
#define F first
#define S second
#define loop(z, v) for (auto &z : v)
#define sp << ' ' <<
#define sps << ' '
#define nl "\\n"
// #define nl endl
#define fat 1000000007
// #define fat 998244353
#define PI_2 1.57079632679489661923

#define N 1000005

int main() {
  ll tt = 1, n, m, l, r, c, i, j, k, w, x, y;
  boost;

  // cin >> tt;
  ll ans = 1001;
  vi spf(N, 0);

  spf[1] = 1;
  for (i = 2; i < N; i++) {
    if (spf[i])
      continue;
    spf[i] = i;

    for (j = i * i; j < N; j += i) {
      if (spf[j] != 0)
        continue;
      spf[j] = i;
    }
  }

  for (int _ = 1; _ <= tt; _++) {
    // cout << "Case #" << _ << ": ";
    cin >> n;
    vi arr(n);
    loop(z, arr) cin >> z;
    bool poss = 1;

    unordered_set<int> primes;
    for (i = n - 1; i >= 0 and poss; i--) {
      r = arr[i];

      while (r > 1) {
        l = spf[r];

        if (primes.find(l) != primes.end()) {
          poss = 0;
          break;
        }
        primes.insert(l);
        c = l;
        while (r > 1 and l == c)
          r = r / l, l = spf[r];
      }
    }

    if (poss)
      cout << "pairwise coprime" << nl;
    else {
      ll gcd = arr[0];
      for (auto &z : arr)
        gcd = __gcd(gcd, z);

      if (gcd == 1)
        cout << "setwise coprime" << nl;
      else
        cout << "not coprime" << nl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
// #define int ll
#define vi vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define pb push_back
#define B begin()
#define E end()
#define sz(A) (ll)(A).size()
#define F first
#define S second
#define loop(z, v) for (auto &z : v)
#define sp << ' ' <<
#define sps << ' '
#define nl "\\n"
// #define nl endl
#define fat 1000000007
// #define fat 998244353
#define PI_2 1.57079632679489661923

#define N 1000005

int main() {
  ll tt = 1, n, m, l, r, c, i, j, k, w, x, y;
  boost;

  // cin >> tt;
  ll ans = 1001;
  vi sieve(N, 0);
  vi sprime;

  for (i = 2; i < N; i++) {
    if (sieve[i])
      continue;
    sprime.push_back(i);

    for (j = i * i; j < N; j += i) {
      sieve[j] = 1;
    }
  }

  for (int _ = 1; _ <= tt; _++) {
    // cout << "Case #" << _ << ": ";
    cin >> n;
    vi arr(n);
    loop(z, arr) cin >> z;
    bool poss = 1;

    unordered_set<int> primes;
    for (i = n - 1; i >= 0 and poss; i--) {
      for (auto &z : sprime) {
        if (arr[i] % z == 0) {
          if (primes.find(z) != primes.end()) {
            // not coprime;
            poss = 0;
            break;
          }
          primes.insert(z);
        }
      }
    }

    if (poss)
      cout << "pairwise coprime" << nl;
    else {
      ll gcd = arr[0];
      for (auto &z : arr)
        gcd = __gcd(gcd, z);

      if (gcd == 1)
        cout << "setwise coprime" << nl;
      else
        cout << "not coprime" << nl;
    }
  }
}