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

#ifndef DEBUG
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
using Graph = vector<vector<Int>>;

const Ld pi = M_PI;
const Int MOD = 1000000007;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> inline T Gcd(T a, T b) {
  T c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}

Int Lcm(Int a, Int b) {
  if (a < b) {
    swap(a, b);
  }

  return a / Gcd(a, b) * b;
}

vector<Int> prime_factor(Int n) {
  map<Int, int> r;
  for (Int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      r[i]++;
      n /= i;
    }
  }
  if (n != 1) {
    r[n] = 1;
  }

  vector<Int> ret(r.size());
  Int ind = 0;
  for (auto [x, y] : r) {
    ret[ind] = x;
    ind++;
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  Int n;
  cin >> n;
  vector<Int> a(n), l(n + 1);
  for (Int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (Int i = 0; i < n; i++) {
    l[i + 1] = Gcd(l[i], a[i]);
  }

  set<Int> s;
  for (Int i = 0; i < n; i++) {
    vector<Int> pf = prime_factor(a[i]);
    bool flag = true;

    for (auto e : pf) {
      if (s.find(e) != s.end()) {
        flag = false;
        break;
      } else {
        s.insert(e);
      }
    }

    if (!flag) {
      if (l.back() == 1) {
        cout << "setwise coprime" << endl;
      } else {
        cout << "not coprime" << endl;
      }

      return 0;
    }
  }

  cout << "pairwise coprime" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#ifndef DEBUG
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
using Graph = vector<vector<Int>>;

const Ld pi = M_PI;
const Int MOD = 1000000007;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> inline T Gcd(T a, T b) {
  T c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}

Int Lcm(Int a, Int b) {
  if (a < b) {
    swap(a, b);
  }

  return a / Gcd(a, b) * b;
}

vector<Int> prime_factor(Int n) {
  vector<Int> ret;
  for (Int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret.push_back(i);
    }
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  Int n;
  cin >> n;
  vector<Int> a(n), l(n + 1);
  for (Int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (Int i = 0; i < n; i++) {
    l[i + 1] = Gcd(l[i], a[i]);
  }

  unordered_set<Int> s;
  for (Int i = 0; i < n; i++) {
    vector<Int> pf = prime_factor(a[i]);
    bool flag = true;

    for (auto e : pf) {
      if (s.find(e) != s.end()) {
        flag = false;
        break;
      } else {
        s.insert(e);
      }
    }

    if (!flag) {
      if (l.back() == 1) {
        cout << "setwise coprime" << endl;
      } else {
        cout << "not coprime" << endl;
      }
    }
  }

  cout << "pairwise coprime" << endl;

  return 0;
}