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

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const int NINF = -1 * INF;
const long long LLINF = 1ll << 60;
const long long LLNINF = -1 * LLINF;

template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ";
  return os << "}";
}

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<vector<int>> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef vector<vld> vvld;
typedef uint_fast16_t fint16;
typedef uint_fast32_t fint32;
typedef uint_fast64_t fint64;

#define mp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define revAll(x) x.rbegin(), x.rend()
#define gcd(a, b) __gcd(a, b)
#define popcount(x) __builtin_popcount(x)
#define isize(x) (int)(x.size())
#define rep(i, n) for (int i = 0; i < n; i++)
#define exists(x, m) m.find(x) != m.end()
#define umap unordered_map
#define uset unordered_set
#define pq priority_queue

const ll MOD = 1e9 + 7;

template <typename T> struct Sieve_Struct {
  const T lim;
  vector<T> primes;
  vector<T> mp;

  Sieve_Struct(T limit) : lim(limit) {
    primes = vector<T>();
    mp = vector<T>(lim + 1, 0);
    for (T num = 2; num <= lim; num++) {
      if (mp[num] == 0)
        primes.pb(num);
      for (int i = 0; i < isize(primes) and primes[i] * num <= lim; i++) {
        mp[num * primes[i]] = primes[i];
        if (num % primes[i] == 0) {
          mp[num] = primes[i];
          break;
        }
      }
    }
  }

  bool is_composite(T x) {
    assert(x <= lim);
    return mp[x] == 0;
  }

  template <typename U> pair<vector<U>, vector<bool>> sieve_range(U L, U R) {
    U sqrtn = ceil(sqrt(R));
    assert(sqrtn <= lim);
    vector<U> primes_range = vector<U>();
    vector<bool> composite_range = vector<bool>(R - L + 1, false);
    if (L == 1)
      composite_range[0] = true;
    for (const U &p : primes) {
      for (U mult = max((U)2, (U)L / p - 10); (ll)mult * p <= R; mult++) {
        ll cand = mult * p;
        if (cand >= L)
          composite_range[cand - L] = true;
      }
    }
    rep(i, isize(composite_range)) {
      if (!composite_range[i])
        primes_range.pb(L + i);
    }
    return {primes_range, composite_range};
  }

  umap<T, int> prime_factorization(T x) {
    assert(x <= lim);
    umap<T, int> pf = umap<T, int>();
    while (x > 1) {
      if (mp[x] == 0) {
        pf[x]++;
        x /= x;
      } else {
        pf[mp[x]]++;
        x /= mp[x];
      }
    }
    return pf;
  }

  vector<T> prime_factors(T x) {
    assert(x <= lim);
    vector<T> factors = vector<T>();
    while (x > 1) {
      T p = mp[x];
      if (p == 0) {
        factors.pb(x);
        break;
      }
      factors.pb(p);
      while (x % p == 0)
        x /= p;
    }
    return factors;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi A = vi();
  int max_a = 0;
  rep(i, n) {
    int x;
    cin >> x;
    A.pb(x);
    max_a = max(max_a, x);
  }
  Sieve_Struct<uint> s = Sieve_Struct<uint>(1e6);
  umap<int, int> prime_freqs = umap<int, int>();
  for (const int &a : A) {
    for (const int &f : s.prime_factors(a)) {
      prime_freqs[f]++;
    }
  }
  int max_freq = 0;
  for (const auto &[factor, freq] : prime_freqs) {
    max_freq = max(max_freq, freq);
  }
  if (max_freq <= 1) {
    cout << "pairwise coprime";
  } else {
    cout << (max_freq < n ? "setwise coprime" : "not coprime");
  }
}

Error: TLE

Fixed submission:

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const int NINF = -1 * INF;
const long long LLINF = 1ll << 60;
const long long LLNINF = -1 * LLINF;

template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ";
  return os << "}";
}

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<vector<int>> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef vector<vld> vvld;
typedef uint_fast16_t fint16;
typedef uint_fast32_t fint32;
typedef uint_fast64_t fint64;

#define mp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define revAll(x) x.rbegin(), x.rend()
#define gcd(a, b) __gcd(a, b)
#define popcount(x) __builtin_popcount(x)
#define isize(x) (int)(x.size())
#define rep(i, n) for (int i = 0; i < n; i++)
#define exists(x, m) m.find(x) != m.end()
#define umap unordered_map
#define uset unordered_set
#define pq priority_queue

const ll MOD = 1e9 + 7;

template <typename T> struct Sieve_Struct {
  const T lim;
  vector<T> primes;
  vector<bool> composite;

  Sieve_Struct(T limit) : lim(limit) {
    primes = vector<T>();
    composite = vector<bool>(lim + 1, false);
    for (T num = 2; num <= lim; num++) {
      if (!composite[num])
        primes.pb(num);
      for (int i = 0; i < isize(primes) and num * primes[i] <= lim; i++) {
        composite[num * primes[i]] = true;
        if (num % primes[i] == 0)
          break;
      }
    }
  }

  bool is_composite(T x) {
    assert(x <= lim);
    return composite[x];
  }

  template <typename U> pair<vector<U>, vector<bool>> sieve_range(U L, U R) {
    U sqrtn = ceil(sqrt(R));
    assert(sqrtn <= lim);
    vector<U> primes_range = vector<U>();
    vector<bool> composite_range = vector<bool>(R - L + 1, false);
    if (L == 1)
      composite_range[0] = true;
    for (const U &p : primes) {
      for (U mult = max((U)2, (U)L / p - 10); (ll)mult * p <= R; mult++) {
        ll cand = mult * p;
        if (cand >= L)
          composite_range[cand - L] = true;
      }
    }
    rep(i, isize(composite_range)) {
      if (!composite_range[i])
        primes_range.pb(L + i);
    }
    return {primes_range, composite_range};
  }

  umap<T, int> prime_factorization(T x) {
    assert(ceil(sqrt(x)) <= lim);
    umap<T, int> pf = umap<T, int>();
    for (const T &p : primes) {
      if (x <= 1)
        break;
      while (x > 1 and x % p == 0) {
        pf[p]++;
        x /= p;
      }
    }
    if (x > 1)
      pf[x]++;
    return pf;
  }

  vector<T> prime_factors(T x) {
    assert(ceil(sqrt(x)) <= lim);
    vector<T> factors = vector<T>();
    for (const T &p : primes) {
      if (x <= 1)
        break;
      if (x % p == 0)
        factors.pb(p);
      while (x > 1 and x % p == 0)
        x /= p;
    }
    if (x > 1)
      factors.pb(x);
    return factors;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi A = vi();
  int max_a = 0;
  rep(i, n) {
    int x;
    cin >> x;
    A.pb(x);
    max_a = max(max_a, x);
  }
  Sieve_Struct<int> s = Sieve_Struct<int>(1e6);
  umap<int, int> prime_freqs = umap<int, int>();
  for (const int &a : A) {
    for (const int &f : s.prime_factors(a)) {
      prime_freqs[f]++;
    }
  }
  int max_freq = 0;
  for (const auto &[factor, freq] : prime_freqs) {
    max_freq = max(max_freq, freq);
  }
  if (max_freq <= 1) {
    cout << "pairwise coprime";
  } else {
    cout << (max_freq < n ? "setwise coprime" : "not coprime");
  }
}