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

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

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const double EPS = 1e-10;

int main() {
  lint N;
  cin >> N;
  vector<lint> A(N);
  rep(i, N) cin >> A[i];

  const lint M = 1001000;
  // const lint M = 20;
  vector<vector<lint>> primes(M + 1, vector<lint>(0));
  for (int p = 2; p <= M; p++) {
    if (primes[p].size() != 0)
      continue;
    for (int pk = p; pk <= M; pk += p) {
      primes[pk].push_back(p);
    }
  }

  vector<lint> min_factor(M, 1);
  for (int p = 2; p <= M; p++) {
    if (min_factor[p] != 1)
      continue;
    for (int pk = p; pk <= M; pk += p) {
      if (min_factor[pk] == 1)
        min_factor[pk] = p;
    }
  }
  // dump(min_factor);

  lint G = 0;
  rep(i, N) G = __gcd(G, A[i]);
  if (G != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  set<lint> used;
  for (int i = 0; i < N; i++) {
    // for (auto p : primes[A[i]]) {
    //     if (used.count(p) == 1) {
    //         cout << "setwise coprime" << endl;
    //         return 0;
    //     } else {
    //         used.insert(p);
    //     }
    // }

    // set<lint> primess{};
    // int a = A[i];
    // if (a == 1) continue;
    // while (a != 1) {
    //     primess.insert(a / min_factor[a]);
    //     a /= min_factor[a];
    // }
    for (auto p : primes[A[i]]) {
      if (used.count(p) == 1) {
        cout << "setwise coprime" << endl;
        return 0;
      } else {
        used.insert(p);
      }
    }
  }

  cout << "pairwise coprime" << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const double EPS = 1e-10;

int main() {
  lint N;
  cin >> N;
  vector<lint> A(N);
  rep(i, N) cin >> A[i];

  const lint M = 1001000;
  // const lint M = 20;
  vector<vector<lint>> primes(M + 1, vector<lint>(0));
  for (int p = 2; p <= M; p++) {
    if (primes[p].size() != 0)
      continue;
    for (int pk = p; pk <= M; pk += p) {
      primes[pk].push_back(p);
    }
  }

  vector<lint> min_factor(M, 1);
  for (int p = 2; p <= M; p++) {
    if (min_factor[p] != 1)
      continue;
    for (int pk = p; pk <= M; pk += p) {
      if (min_factor[pk] == 1)
        min_factor[pk] = p;
    }
  }
  dump(min_factor);

  lint G = 0;
  rep(i, N) G = __gcd(G, A[i]);
  if (G != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  set<lint> used;
  for (int i = 0; i < N; i++) {
    // for (auto p : primes[A[i]]) {
    //     if (used.count(p) == 1) {
    //         cout << "setwise coprime" << endl;
    //         return 0;
    //     } else {
    //         used.insert(p);
    //     }
    // }

    set<lint> primess{};
    int a = A[i];
    while (a != 1) {
      primess.insert(a / min_factor[a]);
      a /= min_factor[a];
    }
    for (auto p : primess) {
      if (used.count(p) == 1) {
        cout << "setwise coprime" << endl;
        return 0;
      } else {
        used.insert(p);
      }
    }
  }

  cout << "pairwise coprime" << endl;

  return 0;
}