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

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vs = vector<string>;
using vvi = vector<vi>;
using vp = vector<pii>;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define each(a, x) for (auto &&a : (x))
#define _overload3(_1, _2, _3, name, ...) name
#define rep1(n) rep2(_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
constexpr int inf = (1 << 30) - 1;
constexpr int mod = 1e9 + 7;
constexpr double eps = 1e-10;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
template <typename T> constexpr void print(const T &t) { cout << t << "\\n"; }
template <typename T, typename U> constexpr bool chmax(T &a, const U &b) {
  if (a < b)
    a = b;
  return a < b;
}
template <typename T, typename U> constexpr bool chmin(T &a, const U &b) {
  if (a > b)
    a = b;
  return a > b;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.se;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << " " << p.se;
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  rep(i, sz(v)) is >> v[i];
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  rep(i, sz(v)) os << v[i] << (i + 1 != sz(v) ? " " : "");
  return os;
}
struct Init {
  Init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init;

// 最大公約数
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

// 素数テーブル　O(NloglogN)
vi eratosthenes(int n) {
  vb primes(n);
  vi D(n);
  rep(i, sz(primes)) primes[i] = true;
  primes[0] = primes[1] = false;
  rep(i, sz(primes) / 2 + 1) {
    if (primes[i]) {
      for (int j = i + i; j < sz(primes); j += i) {
        primes[j] = false;
        D[j] = i;
      }
    }
  }
  return D;
}

// 素因数分解
map<int, int> primeFactorize(int n) {
  map<int, int> prime_factors;
  int m = n;
  rep(i, 2, sqrt(n) + 1) {
    if (m < 1)
      break;
    while (m % i == 0) {
      ++prime_factors[i];
      m /= i;
    }
  }
  if (m != 1)
    ++prime_factors[m];
  return prime_factors;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  cin >> a;
  map<int, int> primes;
  set<int> s;
  int ma = -1;
  int g = a[0];
  rep(i, n) {
    g = gcd(g, a[i]);
    chmax(ma, a[i]);
    s.insert(a[i]);
  }
  if (ma == 1) {
    print("pairwise coprime");
    return 0;
  }

  if (g > 1) {
    print("not coprime");
    return 0;
  }

  if (sz(s) >= 80000) {
    print("setwise coprime");
    return 0;
  }

  rep(i, n) {
    auto pf = primeFactorize(a[i]);
    each(x, pf) primes[x.fi]++;
  }
  ma = -1;
  each(x, primes) chmax(ma, x.se);

  if (ma == 1)
    print("pairwise coprime");

  else
    print("setwise coprime");
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vs = vector<string>;
using vvi = vector<vi>;
using vp = vector<pii>;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define each(a, x) for (auto &&a : (x))
#define _overload3(_1, _2, _3, name, ...) name
#define rep1(n) rep2(_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
constexpr int inf = (1 << 30) - 1;
constexpr int mod = 1e9 + 7;
constexpr double eps = 1e-10;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
template <typename T> constexpr void print(const T &t) { cout << t << "\\n"; }
template <typename T, typename U> constexpr bool chmax(T &a, const U &b) {
  if (a < b)
    a = b;
  return a < b;
}
template <typename T, typename U> constexpr bool chmin(T &a, const U &b) {
  if (a > b)
    a = b;
  return a > b;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.se;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << " " << p.se;
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  rep(i, sz(v)) is >> v[i];
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  rep(i, sz(v)) os << v[i] << (i + 1 != sz(v) ? " " : "");
  return os;
}
struct Init {
  Init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init;

// 最大公約数
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

// 素数テーブル　O(NloglogN)
vi eratosthenes(int n) {
  vb primes(n);
  vi D(n);
  rep(i, sz(primes)) primes[i] = true;
  primes[0] = primes[1] = false;
  rep(i, sz(primes) / 2 + 1) {
    if (primes[i]) {
      for (int j = i + i; j < sz(primes); j += i) {
        primes[j] = false;
        D[j] = i;
      }
    }
  }
  return D;
}

// 素因数分解
map<int, int> primeFactorize(int n) {
  map<int, int> prime_factors;
  int m = n;
  rep(i, 2, sqrt(n) + 1) {
    if (m < 1)
      break;
    while (m % i == 0) {
      ++prime_factors[i];
      m /= i;
    }
  }
  if (m != 1)
    ++prime_factors[m];
  return prime_factors;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  cin >> a;
  vi primes(1000100);
  rep(i, n) {
    map<int, int> pf = primeFactorize(a[i]);
    each(x, pf) primes[x.fi]++;
  }
  bool flag_pairwise = true;
  each(x, primes) {
    if (x >= 2) {
      flag_pairwise = false;
      break;
    }
  }
  bool flag_setwise = false;
  int g = gcd(a[0], a[1]);
  rep(i, 2, n) g = gcd(g, a[i]);
  if (g == 1)
    flag_setwise = true;

  if (flag_pairwise)
    print("pairwise coprime");
  else if (flag_setwise)
    print("setwise coprime");
  else
    print("not coprime");
}