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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;

using ll = long long;

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

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

#include <iostream>
#include <map>
#include <vector>

class Prime {
  // n以下の素数を列挙する
public:
  const int n;
  vector<bool> is_prime;
  vector<int> primes;
  Prime(int size) : n(size), is_prime(n + 1, true) {
    is_prime[0] = false;
    is_prime[1] = false;
    for (int i = 2; i <= n; ++i) {
      if (!is_prime[i])
        continue;
      primes.push_back(i);
      int tmp = 2 * i;
      while (tmp <= n) {
        is_prime[tmp] = false;
        tmp += i;
      }
    }
  }

  bool check(int x) { return is_prime[x]; }
};

struct PrimeFactorization {
  // n*n以下の数についての素因数分解
  const int n;
  Prime p;
  PrimeFactorization(int n) : n(n), p(n) {}

  map<int, int> calc(int a) {
    map<int, int> ret;
    int tmp = a;
    for (int i : p.primes) {
      if (i > tmp)
        break;
      int count = 0;
      while (tmp % i == 0) {
        ++count;
        tmp /= i;
      }
      if (count > 0)
        ret[i] = count;
    }
    if (tmp > 1)
      ret[tmp] = 1;
    return ret;
  }
};

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  bool pairwise = true;
  PrimeFactorization pf(1001);
  vector<bool> primes(1000001);

  int g = -1;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (g == -1)
      g = a;
    auto fact = pf.calc(a);
    for (auto it : fact) {
      if (!pairwise)
        break;
      int d = it.first;
      if (primes[d]) {
        pairwise = false;
      }
      primes[d] = true;
    }
    if (g > 1) {
      g = gcd(a, g);
    }
  }
  bool setwise = (g == 1);
  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwise) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;

using ll = long long;

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

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

#include <iostream>
#include <map>
#include <vector>

class Prime {
  // n以下の素数を列挙する
public:
  const int n;
  vector<bool> is_prime;
  vector<int> primes;
  Prime(int size) : n(size), is_prime(n + 1, true) {
    is_prime[0] = false;
    is_prime[1] = false;
    for (int i = 2; i <= n; ++i) {
      if (!is_prime[i])
        continue;
      primes.push_back(i);
      int tmp = 2 * i;
      while (tmp <= n) {
        is_prime[tmp] = false;
        tmp += i;
      }
    }
  }

  bool check(int x) { return is_prime[x]; }
};

struct PrimeFactorization {
  // n*n以下の数についての素因数分解
  const int n;
  Prime p;
  PrimeFactorization(int n) : n(n), p(n) {}

  map<ll, int> calc(ll a) {
    map<ll, int> ret;
    ll tmp = a;
    for (int i : p.primes) {
      if (i > tmp)
        break;
      int count = 0;
      while (tmp % i == 0) {
        ++count;
        tmp /= i;
      }
      if (count > 0)
        ret[i] = count;
    }
    if (tmp > 1)
      ret[tmp] = 1;
    return ret;
  }
};

int main() {
  int n;
  cin >> n;
  set<ll> nums;
  bool pairwise = true;
  PrimeFactorization pf(1000000);
  vector<bool> primes(1000001);

  ll g = -1;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (g == -1)
      g = a;
    auto fact = pf.calc(a);
    for (auto it : fact) {
      if (!pairwise)
        break;
      ll d = it.first;
      if (primes[d]) {
        pairwise = false;
      }
      primes[d] = true;
    }
    g = gcd(a, g);
  }
  bool setwise = (g == 1);
  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwise) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}