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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X)                                                                  \\
  ({                                                                           \\
    if (1)                                                                     \\
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \\
  })

vl primes;
map<ll, ll> primeSet;
vvl buckets;
vl arr;
ll N;
vector<bool> thing;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  primes = vl();
  thing = vector<bool>(1000001, true);
  FOR(i, 2, 1000001) {
    if (!thing[i])
      continue;
    primes.pb(i);
    primeSet[i] = primes.size() - 1;
    ll s = i;
    while (s < thing.size()) {
      thing[s] = false;
      s += i;
    }
  }
  cin >> N;
  arr = vl(N, 0);

  FOR(i, 0, N) cin >> arr[i];
  buckets = vvl(primes.size(), vl());
  FOR(i, 0, N) {
    ll j = 0;
    ll A = arr[i];
    if (primeSet.find(A) != primeSet.end()) {
      buckets[primeSet[A]].pb(i);
      A = 1;
    }

    while (A > 1) {
      if (A % primes[j] == 0) {
        buckets[j].pb(i);
        while (A % primes[j] == 0)
          A = A / primes[j];
      }
      if (primeSet.find(A) != primeSet.end()) {
        buckets[primeSet[A]].pb(i);
        A = 1;
      }
      j++;
    }
  }

  bool pairwise = true;
  FOR(i, 0, buckets.size()) {
    if (buckets[i].size() > 1)
      pairwise = false;
  }

  bool setwise = true;
  FOR(i, 0, buckets.size()) {
    if (buckets[i].size() == N)
      setwise = false;
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwise) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X)                                                                  \\
  ({                                                                           \\
    if (1)                                                                     \\
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \\
  })

vl primes;
map<ll, ll> primeSet;
vvl buckets;
vl arr;
ll N;
vector<bool> thing;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  primes = vl();
  thing = vector<bool>(1000001, true);
  FOR(i, 2, 1000001) {
    if (!thing[i])
      continue;
    primes.pb(i);
    primeSet[i] = primes.size() - 1;
    ll s = i;
    while (s < thing.size()) {
      thing[s] = false;
      s += i;
    }
  }
  cin >> N;
  arr = vl(N, 0);

  FOR(i, 0, N) cin >> arr[i];
  buckets = vvl(primes.size(), vl());
  FOR(i, 0, N) {
    ll j = 0;
    ll A = arr[i];
    if (primeSet.find(A) != primeSet.end()) {
      buckets[primeSet[A]].pb(i);
      continue;
    }

    while (A > 1) {
      if (A % primes[j] == 0) {
        buckets[j].pb(i);
        while (A % primes[j] == 0)
          A = A / primes[j];
      }
      j++;
    }
  }

  bool pairwise = true;
  FOR(i, 0, buckets.size()) {
    if (buckets[i].size() > 1)
      pairwise = false;
  }

  bool setwise = true;
  FOR(i, 0, buckets.size()) {
    if (buckets[i].size() == N)
      setwise = false;
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwise) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}