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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (int64_t)(n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)

vector<bool> eratosthenes_sieve(size_t size) {
  vector<bool> is_prime(size + 1, true);
  for (size_t i = 0; i <= size; i++) {
    if (i < 2) {
      is_prime[i] = false;
    } else if (is_prime[i]) {
      for (size_t j = 2 * i; j <= size; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return move(is_prime);
}

int main() {
  int N;
  cin >> N;

  constexpr int MAXFACTOR = 1'000'000;
  auto sieve = eratosthenes_sieve(MAXFACTOR);
  vector<int> primes;
  vector<int> idx(sieve.size());
  irep(i, MAXFACTOR) {
    if (sieve[i]) {
      idx[i] = primes.size();
      primes.push_back(i);
    }
  }

  vector<int> num_factor(primes.size(), 0);
  rep(i, N) {
    int a;
    cin >> a;

    for (int i = 0; i < primes.size() and primes[i] * primes[i] <= a; i++) {
      if (a % primes[i] == 0) {
        num_factor[i]++;
        while (a % primes[i] == 0) {
          a /= primes[i];
        }
      }
    }
    if (sieve[a]) {
      num_factor[idx[a]]++;
    }
  }

  int result = 0;
  for (auto &elm : num_factor) {
    chmax(result, elm);
  }

  cout << (result <= 1 ? "pairwise coprime"
                       : (result < N ? "setwise coprime" : "not coprime"))
       << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (int64_t)(n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)

vector<bool> eratosthenes_sieve(size_t size) {
  vector<bool> is_prime(size + 1, true);
  for (size_t i = 0; i <= size; i++) {
    if (i < 2) {
      is_prime[i] = false;
    } else if (is_prime[i]) {
      for (size_t j = 2 * i; j <= size; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return move(is_prime);
}

int main() {
  int N;
  cin >> N;

  constexpr int MAXFACTOR = 1'000'000;
  auto sieve = eratosthenes_sieve(MAXFACTOR);
  vector<int> primes;
  vector<int> idx(sieve.size());
  irep(i, MAXFACTOR) {
    if (sieve[i]) {
      idx[i] = primes.size();
      primes.push_back(i);
    }
  }

  vector<int> num_factor(primes.size(), 0);
  rep(i, N) {
    int a;
    cin >> a;

    for (int i = 0; i < primes.size(); i++) {
      if (a % primes[i] == 0) {
        num_factor[i]++;
      }
    }
  }

  int result = 0;
  for (auto &elm : num_factor) {
    chmax(result, elm);
  }

  cout << (result <= 1 ? "pairwise coprime"
                       : (result < N ? "setwise coprime" : "not coprime"))
       << endl;

  return 0;
}