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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \\
  ASC((x));                                                                    \\
  REV((x))
#define pb push_back
#define eb emplace_back

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int main() {
  ll N;
  cin >> N;
  vec A(N);
  REP(i, 0, N) cin >> A[i];

  ASC(A);
  ll setG = A[0];
  REP(i, 1, N) setG = gcd(A[i], setG);

  vec P(A[N - 1] + 1, 1);
  REP(i, 0, A[N - 1] + 1) P[i] = i;
  for (ll i = 2; i * i <= A[N - 1]; ++i) {
    if (P[i] == i) {
      for (ll j = 2; i * j <= A[N - 1]; ++j) {
        if (P[i * j] == 1)
          P[i * j] = i;
        else
          P[i * j] = MIN(i, P[i * j]);
      }
    }
  }

  mat Q(N);
  REP(i, 0, N) {
    ll k = A[i];
    while (k > 1) {
      if (SZ(Q[i]) == 0)
        Q[i].pb(P[k]);
      else if (Q[i].back() != P[k])
        Q[i].pb(P[k]);
      k /= P[k];
    }
  }

  bool f = true;
  map<ll, ll> R;
  REP(i, 0, N) { FORR(v, Q[i])++ R[v]; }
  FORR(r, R) {
    if (r.second > 1)
      f = false;
  }

  if (f)
    PR("pairwise coprime");
  else {
    if (setG == 1)
      PR("setwise coprime");
    else
      PR("not coprime");
  }

  return 0;
}

/*



*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \\
  ASC((x));                                                                    \\
  REV((x))
#define pb push_back
#define eb emplace_back

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int main() {
  ll N;
  cin >> N;
  vec A(N);
  REP(i, 0, N) cin >> A[i];

  ASC(A);
  ll setG = A[0];
  REP(i, 1, N) setG = gcd(A[i], setG);

  vec P(A[N - 1] + 1, 1);
  map<ll, vec> Q;
  for (ll i = 2; i * i <= A[N - 1]; ++i) {
    if (P[i] == 1) {
      for (ll j = 2; i * j <= A[N - 1]; ++j) {
        if (P[i * j] == 1)
          P[i * j] = i;
        else
          P[i * j] = MIN(i, P[i * j]);
        if (find(ALL(A), i * j) != A.end())
          Q[i * j].pb(P[i * j]);
      }
    }
  }

  bool f = true;
  map<ll, ll> R;
  FORR(q, Q) { FORR(v, q.second)++ R[v]; }
  FORR(r, R) {
    if (r.first > 1 && r.second > 1) {
      f = false;
      break;
    }
  }

  if (f)
    PR("pairwise coprime");
  else {
    if (setG == 1)
      PR("setwise coprime");
    else
      PR("not coprime");
  }

  return 0;
}

/*



*/