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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define MAX 1000001

ll _sieve_size; // ll is defined as: typedef long long ll;
bitset<MAX> bs; // 10^7 should be enough for most cases
bitset<MAX> used;
vi primes;                      // compact list of primes in form of vector<int>
void sieve(ll upperbound) {     // create list of primes in [0..upperbound]
  _sieve_size = upperbound + 1; // add 1 to include upperbound
  bs.set();                     // set all bits to 1
  bs[0] = bs[1] = 0;            // except index 0 and 1
  for (ll i = 2; i <= _sieve_size; i++)
    if (bs[i]) {
      // cross out multiples of i starting from i * i!
      for (ll j = i * i; j <= _sieve_size; j += i)
        bs[j] = 0;
      primes.push_back((int)i); // add this prime to the list of primes
    }
}

bool prime_factors(int n) {
  vi pfs;
  int pf = 0;
  int pfactor = primes[pf];
  while (pfactor * pfactor <= n) {
    if (n % pfactor == 0) {
      pfs.push_back(pfactor);
      if (used[pfactor])
        return true;
      used[pfactor] = 1;
    }
    while (n % pfactor == 0) {
      n /= pfactor;
    }
    pfactor = primes[++pf];
  }
  if (n != 1) {
    pfs.push_back(n);
    if (used[n])
      return true;
    used[n] = 1;
  }
  return false;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  sieve(MAX);
  int n;
  cin >> n;
  int a[n];
  bool isPairWise = true;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (prime_factors(a[i])) {
      isPairWise = false;
      break;
    }
  }

  int mcd = a[0];
  for (int i = 1; i < n; i++) {
    mcd = gcd(mcd, a[i]);
  }

  if (isPairWise) {
    cout << "pairwise coprime";
  } else if (mcd == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define MAX 1000001

ll _sieve_size; // ll is defined as: typedef long long ll;
bitset<MAX> bs; // 10^7 should be enough for most cases
bitset<MAX> used;
vi primes;                      // compact list of primes in form of vector<int>
void sieve(ll upperbound) {     // create list of primes in [0..upperbound]
  _sieve_size = upperbound + 1; // add 1 to include upperbound
  bs.set();                     // set all bits to 1
  bs[0] = bs[1] = 0;            // except index 0 and 1
  for (ll i = 2; i <= _sieve_size; i++)
    if (bs[i]) {
      // cross out multiples of i starting from i * i!
      for (ll j = i * i; j <= _sieve_size; j += i)
        bs[j] = 0;
      primes.push_back((int)i); // add this prime to the list of primes
    }
}

bool prime_factors(int n) {
  vi pfs;
  int pf = 0;
  int pfactor = primes[pf];
  while (n > 1) {
    if (n % pfactor == 0) {
      pfs.push_back(pfactor);
      if (used[pfactor])
        return true;
      used[pfactor] = 1;
    }
    while (n % pfactor == 0) {
      n /= pfactor;
    }
    pf++;
    pfactor = primes[pf];
  }
  return false;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  sieve(MAX);
  int n;
  cin >> n;
  int a[n];
  bool isPairWise = true;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (prime_factors(a[i])) {
      isPairWise = false;
      break;
    }
  }

  int mcd = a[0];
  for (int i = 1; i < n; i++) {
    mcd = gcd(mcd, a[i]);
  }

  if (isPairWise) {
    cout << "pairwise coprime";
  } else if (mcd == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}