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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> ll size(const T &x) { return x.size(); }
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define iter(it, c) for (auto it = (c).begin(); it != (c).end(); ++it)
#define pb push_back
#define fs first
#define sc second
#define MOD 1000000007
#define MOD2 998244353
#define INF 1000000000000000000ll
#pragma GCC optimize("O1")
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
template <class T> T smod(T a, T b) { return (a % b + b) % b; }

#define MAXN 1000010

int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<int> getFactorization(int x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

int cnt[1000010] = {0};
int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  /*ll t;
  cin >> t;
  while(t--)*/
  int n;
  sieve();
  cin >> n;
  vector<int> A(n);
  rep(i, 0, n) { cin >> A[i]; }

  set<int> used;
  rep(i, 0, n) {
    vector<int> ans = getFactorization(A[i]);
    set<ll> ans2;
    rep(i, 0, ans.size()) { ans2.insert(ans[i]); }
    iter(it, ans2) {
      used.insert(*it);
      cnt[*it]++;
    }
  }
  bool is_coprime = true;
  bool is_setprime = true;
  iter(it, used) {
    int val = cnt[(*it)];
    if (val > 1) {
      is_coprime = false;
    }

    if (val == n) {
      is_setprime = false;
    }
  }
  if (is_coprime) {
    cout << "pairwise coprime" << '\\n';
    return 0;
  }
  if (is_setprime) {
    cout << "setwise coprime" << '\\n';
    return 0;
  }
  cout << "not coprime" << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> ll size(const T &x) { return x.size(); }
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define iter(it, c) for (auto it = (c).begin(); it != (c).end(); ++it)
#define pb push_back
#define fs first
#define sc second
#define MOD 1000000007
#define MOD2 998244353
#define INF 1000000000000000000ll
#pragma GCC optimize("O1")
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
template <class T> T smod(T a, T b) { return (a % b + b) % b; }

int cnt[1000010] = {0};
int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  /*ll t;
  cin >> t;
  while(t--)*/
  int n;
  cin >> n;
  vector<int> A(n);
  rep(i, 0, n) { cin >> A[i]; }

  set<int> used;
  rep(i, 0, n) {
    set<int> ans;
    ll a = A[i];
    rep(i, 2, sqrt(a) + 1) {
      while (a % i == 0) {
        ans.insert(i);
        a /= i;
      }
    }
    if (a != 1) {
      ans.insert(a);
    }
    // cerr << A[i] << " primes: ";
    iter(it, ans) {
      int val = *it;
      used.insert(val);
      // cout << *it << endl;
      // cerr << cnt_ai[j] << ' ';
      cnt[val]++;
      if (cnt[val] == n) {
        break;
      }
    } // cerr << '\\n';
  }
  bool is_coprime = true;
  bool is_setprime = true;
  iter(it, used) {
    int val = cnt[(*it)];
    if (val > 1) {
      is_coprime = false;
    }

    if (val == n) {
      is_setprime = false;
    }
  }
  if (is_coprime) {
    cout << "pairwise coprime" << '\\n';
    return 0;
  }
  if (is_setprime) {
    cout << "setwise coprime" << '\\n';
    return 0;
  }
  cout << "not coprime" << '\\n';
  return 0;
}