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

#include <bits/stdc++.h>
using namespace std;
#define lundi                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define all(x) x.begin(), x.end()

struct Sieve {
  vector<bool> sieve;
  vector<int> primes;

  Sieve(int n) {
    sieve.assign(n + 1, true);
    if (n >= 2) {
      sieve[0] = sieve[1] = false;
    }
    int rt = sqrt(n);
    for (int i = 4; i <= n; i += 2)
      sieve[i] = false;
    for (int i = 3; i <= n; i += 2) {
      if (sieve[i]) {
        for (int j = i * 3; j <= n; j += (2 * i)) {
          sieve[j] = false;
        }
      }
    }
  }

  bool isPrime(int x) {
    if (x >= sieve.size()) {
      cout << "Out of sieve range" << endl;
      assert(x >= sieve.size());
    }
    return sieve[x];
  }

  void generatePrimes() {
    primes.clear();
    for (int i = 2; i < sieve.size(); i++) {
      if (sieve[i])
        primes.push_back(i);
    }
  }
};

void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  int gcd = -1;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (gcd == -1) {
      gcd = v[i];
    } else {
      gcd = __gcd(v[i], gcd);
    }
  }

  if (gcd != 1) {
    cout << "not coprime" << endl;
    return;
  }

  Sieve sieve(1e6 + 10);
  sieve.generatePrimes();
  vector<bool> taken(sieve.primes.size(), false);
  for (int i = 0; i < v.size(); i++) {
    int pind = 0;
    while (v[i] > 1 && pind < sieve.primes.size() &&
           sieve.primes[pind] * sieve.primes[pind] <= v[i]) {

      if (v[i] % sieve.primes[pind] == 0 && taken[pind]) {
        cout << "setwise coprime\\n";
        return;
      }
      while (v[i] > 1 && v[i] % sieve.primes[pind] == 0) {
        taken[pind] = true;
        v[i] /= sieve.primes[pind];
      }

      pind++;
    }

    if (sieve.isPrime(v[i])) {
      int ind = lower_bound(all(sieve.primes), v[i]) - sieve.primes.begin();
      if (taken[ind]) {
        cout << "setwise coprime\\n";
        return;
      }
      taken[ind] = true;
    }
  }

  cout << "pairwise coprime\\n";
}

signed main() {
  lundi;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lundi                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

struct Sieve {
  vector<bool> sieve;
  vector<int> primes;

  Sieve(int n) {
    sieve.assign(n + 1, true);
    if (n >= 2) {
      sieve[0] = sieve[1] = false;
    }
    int rt = sqrt(n);
    for (int i = 4; i <= n; i += 2)
      sieve[i] = false;
    for (int i = 3; i <= n; i += 2) {
      if (sieve[i]) {
        for (int j = i * 3; j <= n; j += (2 * i)) {
          sieve[j] = false;
        }
      }
    }
  }

  bool isPrime(int x) {
    if (x >= sieve.size()) {
      cout << "Out of sieve range" << endl;
      assert(x >= sieve.size());
    }
    return sieve[x];
  }

  void generatePrimes() {
    primes.clear();
    for (int i = 2; i < sieve.size(); i++) {
      if (sieve[i])
        primes.push_back(i);
    }
  }
};

void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  int gcd = -1;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (gcd == -1) {
      gcd = v[i];
    } else {
      gcd = __gcd(v[i], gcd);
    }
  }

  if (gcd != 1) {
    cout << "not coprime" << endl;
    return;
  }

  Sieve sieve(1e6 + 10);
  sieve.generatePrimes();
  vector<bool> taken(sieve.primes.size(), false);
  for (int i = 0; i < v.size(); i++) {
    int pind = 0;
    while (v[i] > 1 && pind < sieve.primes.size() &&
           sieve.primes[pind] <= v[i]) {

      if (v[i] % sieve.primes[pind] == 0 && taken[pind]) {
        cout << "setwise coprime\\n";
        return;
      }
      while (v[i] > 1 && v[i] % sieve.primes[pind] == 0) {
        taken[pind] = true;
        v[i] /= sieve.primes[pind];
      }

      pind++;
    }
  }
  cout << "pairwise coprime\\n";
}

signed main() {
  lundi;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}