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

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
template <typename T> vector<T> smallest_prime_factors(T n) {

  vector<T> spf(n + 1);
  for (int 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) {

        // iを持つ整数かつまだ素数が決まっていないなら
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }

  return spf;
}

template <typename T> vector<T> factolization(T x, vector<T> &spf) {
  vector<T> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x /= spf[x];
  }
  sort(ret.begin(), ret.end());
  return ret;
}
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

signed main() {

  constexpr int MAX = 1000000;
  int N;
  cin >> N;
  auto spf = smallest_prime_factors(MAX);
  vector<int> a(N);
  rep(i, N) cin >> a[i];
  int g = a[0];
  for (int i = 1; i < N; i++) {
    g = gcd(g, a[i]);
  }
  if (g != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  vector<int> cnt(1100000);
  rep(i, N) {
    int x = a[i];
    auto result = factolization(x, spf);
    set<int> st;
    for (auto z : result) {
      st.insert(z);
    }
    for (auto z : st) {
      if (cnt[z]) {
        cout << "setwise coprime" << endl;
        return 0;
      }
      cnt[z] = 1;
    }
  }
  cout << "pairwise coprime" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int g = a[0];
  for (int i = 1; i < n; i++) {
    g = gcd(g, a[i]);
  }
  if (g != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  vector<int> vis(1100000);
  for (int i = 2; i < 1100000; i++) {
    for (int j = i + i; j < 1100000; j += i) {
      if (vis[i])
        continue;
      vis[j] = 1;
    }
  }
  // rep(i,10)trace(vis[i]);
  vis[0] = 1;
  vis[1] = 1;
  vector<int> res;
  rep(i, 1100000) if (!vis[i]) res.push_back(i);
  // trace(res);
  vector<int> cnt(1100000);
  rep(i, n) {
    int x = a[i];
    for (auto p : res) {
      if (x % p == 0) {
        if (cnt[x]) {
          cout << "setwise coprime" << endl;
          return 0;
        }
        cnt[x] = 1;
      }
    }
  }
  cout << "pairwise coprime" << endl;

  return 0;
}