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

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

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<vector<int>>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const ll MOD = 1e9 + 7;

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

template <class T> struct PrimeFact {
  vector<T> spf;
  PrimeFact(T N) { init(N); }
  void init(T N) {
    spf.assign(N + 1, 0);
    for (T i = 0; i <= N; i++)
      spf[i] = i;
    for (T i = 2; i * i <= N; i++) {
      if (spf[i] == i) {
        for (T j = i * i; j <= N; j += i) {
          if (spf[j] == j) {
            spf[j] = i;
          }
        }
      }
    }
  }
  map<T, T> get(T n) {
    map<T, T> mp;
    while (n != 1) {
      mp[spf[n]]++;
      n /= spf[n];
    }
    return mp;
  }
};

int main() {
  int N;
  cin >> N;
  vi A(N);
  for (auto &x : A)
    cin >> x;

  int G = A[0];
  for (int i = 1; i < N; i++) {
    G = __gcd(G, A[i]);
  }

  PrimeFact<int> p(1001001);
  vi cnt(1001001, 0);
  bool ok = true;
  for (int i = 0; i < N; i++) {
    while (A[i] != 1) {
      int val = p.spf[A[i]];
      if (cnt[val])
        ok = false;
      cnt[p.spf[A[i]]]++;
      while (p.spf[A[i] / val] == val) {
        A[i] /= val;
      }
      A[i] /= val;
    }
  }
  if (ok) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  if (G == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<vector<int>>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const ll MOD = 1e9 + 7;

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

template <class T> struct PrimeFact {
  vector<T> spf;
  PrimeFact(T N) { init(N); }
  void init(T N) {
    spf.assign(N + 1, 0);
    for (T i = 0; i <= N; i++)
      spf[i] = i;
    for (T i = 2; i * i <= N; i++) {
      if (spf[i] == i) {
        for (T j = i * i; j <= N; j += i) {
          if (spf[j] == j) {
            spf[j] = i;
          }
        }
      }
    }
  }
  map<T, T> get(T n) {
    map<T, T> mp;
    while (n != 1) {
      mp[spf[n]]++;
      n /= spf[n];
    }
    return mp;
  }
};

int main() {
  int N;
  cin >> N;
  vi A(N);
  for (auto &x : A)
    cin >> x;

  int G = A[0];
  for (int i = 1; i < N; i++) {
    G = __gcd(G, A[i]);
  }

  PrimeFact<int> p(1001001);
  vi cnt(1001001, 0), flag(1001001, 0);
  bool ok = true;
  for (int i = 0; i < N; i++) {
    flag.assign(1001001, 0);
    while (A[i] != 1) {
      if (!flag[p.spf[A[i]]] && cnt[p.spf[A[i]]])
        ok = false;
      cnt[p.spf[A[i]]]++;
      flag[p.spf[A[i]]] = true;
      A[i] /= p.spf[A[i]];
    }
  }
  if (ok) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  if (G == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}