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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define vec vector
#define fi first
#define se second
#define eb emplace_back
#define ef emplace_front
#define pof pop_front()
#define pob pop_back()
#define Rep(i, n) for (li i = 0, nn = (li)(n); i != nn; ++i)
#define Rep1(i, n) for (li i = 1, nn = (li)(n) + 1; i != nn; ++i)
#define Size(v) (li)(v.size())
#define SortA(v) sort(v.begin(), v.end())
#define SortD(v) sort(v.rbegin(), v.rend())
#define All(a) (a).begin(), (a).end()
#define Input(a)                                                               \\
  for (li ii = 0, nn = (li)(a.size()); ii != nn; ++ii) {                       \\
    cin >> (a).at(ii);                                                         \\
  }
#define Input2(a, b)                                                           \\
  for (li ii = 0, nn = (li)(a.size()); ii != nn; ++ii) {                       \\
    cin >> (a).at(ii) >> (b).at(ii);                                           \\
  }
#define mp make_pair
using li = int64_t;
using intl = int64_t;
using vi = vec<li>;
using vb = vec<bool>;
using vvi = vec<vi>;
using qi = deque<li>;
using pi = pair<li, li>;
using vpi = vec<pi>;
using si = set<li>;
using vsi = vec<si>;
using mii = map<li, li>;
using vmii = vec<mii>;
vec<li> dx = {0, 1, 0, -1, 1, 1, -1, -1};
vec<li> dy = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr li INF = LLONG_MAX;
constexpr li mod = 1000000007;

void Yes(li b) {
  if (b)
    cout << "Yes";

  else
    cout << "No";
}
void yes(li b) {
  if (b)
    cout << "yes";
  else
    cout << "no";
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout.precision(16);

  li N;
  cin >> N;

  vector<int> A(N);

  Input(A);

  vector<int> primeset;

  for (int i = 2; i <= 1000; i++) {
    bool isp = 1;
    for (int j = 2; j * j <= i; j++) {
      if (!(i % j)) {
        isp = 0;
        break;
      }
    }
    if (isp)
      primeset.eb(i);
  }

  int GCD = A[0];
  set<int> foundp;
  bool ispcop = 1;

  for (auto &&a : A) {
    GCD = __gcd(GCD, a);

    if (ispcop) {

      for (auto &&p : primeset) {
        if (a < p)
          break;
        if (!(a % p)) {
          if (foundp.count(p)) {
            ispcop = 0;
            break;
          } else {
            foundp.insert(p);
          }
        }

        while (!(a % p)) {
          a /= p;
        }
      }

      if (a != 1) {
        if (foundp.count(a)) {
          ispcop = 0;
          break;
        } else {
          foundp.insert(a);
        }
      }
    }
  }

  if (ispcop) {
    cout << "pairwise coprime";
  } else if (GCD == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define vec vector
#define fi first
#define se second
#define eb emplace_back
#define ef emplace_front
#define pof pop_front()
#define pob pop_back()
#define Rep(i, n) for (li i = 0, nn = (li)(n); i != nn; ++i)
#define Rep1(i, n) for (li i = 1, nn = (li)(n) + 1; i != nn; ++i)
#define Size(v) (li)(v.size())
#define SortA(v) sort(v.begin(), v.end())
#define SortD(v) sort(v.rbegin(), v.rend())
#define All(a) (a).begin(), (a).end()
#define Input(a)                                                               \\
  for (li ii = 0, nn = (li)(a.size()); ii != nn; ++ii) {                       \\
    cin >> (a).at(ii);                                                         \\
  }
#define Input2(a, b)                                                           \\
  for (li ii = 0, nn = (li)(a.size()); ii != nn; ++ii) {                       \\
    cin >> (a).at(ii) >> (b).at(ii);                                           \\
  }
#define mp make_pair
using li = int64_t;
using intl = int64_t;
using vi = vec<li>;
using vb = vec<bool>;
using vvi = vec<vi>;
using qi = deque<li>;
using pi = pair<li, li>;
using vpi = vec<pi>;
using si = set<li>;
using vsi = vec<si>;
using mii = map<li, li>;
using vmii = vec<mii>;
vec<li> dx = {0, 1, 0, -1, 1, 1, -1, -1};
vec<li> dy = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr li INF = LLONG_MAX;
constexpr li mod = 1000000007;

void Yes(li b) {
  if (b)
    cout << "Yes";

  else
    cout << "No";
}
void yes(li b) {
  if (b)
    cout << "yes";
  else
    cout << "no";
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout.precision(16);

  li N;
  cin >> N;

  vi A(N);

  Input(A);

  vi primeset;

  for (int i = 2; i <= 1000; i++) {
    bool isp = 1;
    for (int j = 2; j * j <= i; j++) {
      if (!(i % j))
        isp = 0;
      break;
    }
    if (isp)
      primeset.eb(i);
  }

  li GCD = A[0];
  si foundp;
  bool ispcop = 1;

  for (auto &&a : A) {
    GCD = __gcd(GCD, a);

    if (ispcop) {

      for (auto &&p : primeset) {
        if (!(a % p)) {
          if (foundp.count(p)) {
            ispcop = 0;
            break;
          } else {
            foundp.insert(p);
          }
        }

        while (!(a % p)) {
          a /= p;
        }
      }

      if (a != 1) {
        if (foundp.count(a)) {
          ispcop = 0;
          break;
        } else {
          foundp.insert(a);
        }
      }
    }
  }

  if (ispcop) {
    cout << "pairwise coprime";
  } else if (GCD == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}