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

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define fo(i, n) for (long long i = 0; i < n; i++)
#define ll long long int
#define inf 999999999999999999
#define pb push_back
#define fi first
#define se second
#define vi vector<ll>
#define vii vector<vector<ll>>
#define pi pair<ll, ll>
#define mp make_pair
#define MOD 1000000007
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); ++it)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
using namespace std;
// cerr<< '\\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << "
// ms\\n" ; freopen("explicit.in", "r", stdin); freopen("explicit.out", "w",
// stdout);

void SieveOfEratosthenes() {
  bool prime[10000001];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= 10000000; p++) {
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= 10000000; i += p) {
        prime[i] = false;
      }
    }
  }
}
ll leastprime[1000005] = {0};
void leastPrimeFactor(ll n) {
  ll i, j;
  leastprime[1] = 1;
  for (i = 2; i <= n; i++) {
    if (leastprime[i] == 0) {
      leastprime[i] = i;
      for (j = 2 * i; j <= n; j = j + i) {
        if (leastprime[j] == 0) {
          leastprime[j] = i;
        }
      }
    }
  }
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b % a == 0)
    return a;
  return gcd(b % a, a);
}
ll nCrModpDP(ll n, ll r, ll p) {
  ll C[r + 1];
  memset(C, 0, sizeof(C));
  C[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, r); j > 0; j--)
      C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}
ll nCrModp(ll n, ll r, ll p) {
  // Base case
  if (r == 0)
    return 1;
  ll ni = n % p, ri = r % p;

  return (nCrModp(n / p, r / p, p) * nCrModpDP(ni, ri, p)) % p;
}
int32_t main() {
  fio ll j, i, n, t, l, r, c, h, k, m, maxi, d, x;
  cin >> n;
  ll co = 1, se = 1;
  ll a[n];
  for (j = 0; j < n; j++)
    cin >> a[j];
  c = a[0];
  for (j = 1; j < n; j++) {
    c = gcd(a[j], c);
  }
  if (c != 1)
    se = -1;
  leastPrimeFactor(1000001);
  ll prime[1000001] = {0};
  for (j = 0; j < n; j++) {
    k = a[j];
    d = leastprime[k];
    // cout<<d<<endl;
    set<ll> s;
    while (k != 1) {
      s.insert(d);
      k = k / d;
      d = leastprime[k];
    }
    tr(s, it) {
      prime[*it]++;
      if (prime[*it] >= 2)
        co = -1;
    }
  }
  if (co == 1)
    cout << "pairwise coprime";
  else if (se == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define fo(i, n) for (long long i = 0; i < n; i++)
#define ll long long int
#define inf 999999999999999999
#define pb push_back
#define fi first
#define se second
#define vi vector<ll>
#define vii vector<vector<ll>>
#define pi pair<ll, ll>
#define mp make_pair
#define MOD 1000000007
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); ++it)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
using namespace std;
// cerr<< '\\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << "
// ms\\n" ; freopen("explicit.in", "r", stdin); freopen("explicit.out", "w",
// stdout);
bool prime[1000001];
void SieveOfEratosthenes(ll n) {

  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= n; i += p) {
        prime[i] = false;
      }
    }
  }
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b % a == 0)
    return a;
  return gcd(b % a, a);
}
ll nCrModpDP(ll n, ll r, ll p) {
  ll C[r + 1];
  memset(C, 0, sizeof(C));
  C[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, r); j > 0; j--)
      C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}
ll nCrModp(ll n, ll r, ll p) {
  // Base case
  if (r == 0)
    return 1;
  ll ni = n % p, ri = r % p;

  return (nCrModp(n / p, r / p, p) * nCrModpDP(ni, ri, p)) % p;
}
vector<ll> g[1000005];
int32_t main() {
  fio ll j, i, n, t, l, r, c, h, k, e, o, m, maxi, d, x;
  cin >> n;
  ll a[n];
  set<ll> s;
  m = 0;
  e = 0;
  o = 0;
  for (j = 0; j < n; j++) {
    cin >> a[j];
    if (a[j] % 2 == 0)
      e++;
    else {
      s.insert(a[j]);
      m = max(m, a[j]);
      o++;
    }
  }
  SieveOfEratosthenes(m);

  vector<ll> p, np;
  for (j = 1; j <= m; j++) {
    if (prime[j])
      p.pb(j);
  }
  // cout<<p[0];
  for (j = 1; j < p.size(); j++) {
    c = 0;
    tr(s, it) {
      c++;
      if ((*it) % p[j] == 0) {
        g[p[j]].pb(c);
      }
    }
  }
  ll co = 1, se = 1;
  if (e >= 2) {
    co = -1;
  }
  // cout<<e<<endl;
  if (e == n) {
    se = -1;
  }
  for (i = 0; i < p.size(); i++) {
    if (g[p[i]].size() >= 2) {
      co = -1;
    }
    if (g[p[i]].size() == n) {
      se = -1;
    }
  }
  if (co == 1)
    cout << "pairwise coprime";
  else if (se == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}