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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

using Pair = pair<int, int>;
using lint = long long;

const int mod = 1e9 + 7;
// const int mod = 998244353;

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#define rep(i, n) for (int i = 0; i < (n); i++)
// 2数の最大公約数,O(log max(a,b))
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// 2数の最小公倍数,O(log max(a,b))
long long lcm(long long i, long long j) { return i * (j / gcd(i, j)); }
int main() {
  int n;
  cin >> n;
  vector<lint> a(n);
  rep(i, n) cin >> a[i];
  vector<lint> prime, d(1000010);
  for (int i = 2; i <= 1000000; i++) {
    bool is = true;
    for (int j = 0; j < prime.size(); j++) {
      if (i % prime[j] == 0) {
        is = false;
        d[i] = j;
        break;
      }
      if (prime[j] * prime[j] > i)
        break;
    }
    if (is) {
      prime.push_back(i);
      d[i] = prime.size() - 1;
    }
  }
  vector<bool> inc(prime.size(), false);
  bool ret1 = true, ret2 = true;
  rep(i, n) {
    int e = a[i];
    int before = mod;
    while (e > 1) {
      if (before != prime[d[e]] && inc[d[e]])
        ret1 = false;
      inc[d[e]] = true;
      before = prime[d[e]];
      e /= prime[d[e]];
    }
  }
  int gc = a[0];
  rep(i, n) gc = gcd(gc, a[i]);
  if (gc != 1)
    ret2 = false;
  if (ret1)
    cout << "pairwise coprime" << endl;
  else if (ret2)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

using Pair = pair<int, int>;
using lint = long long;

// const int mod = 1e9 + 7;
// const int mod = 998244353;

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#define rep(i, n) for (int i = 0; i < (n); i++)
// 2数の最大公約数,O(log max(a,b))
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// 2数の最小公倍数,O(log max(a,b))
long long lcm(long long i, long long j) { return i * (j / gcd(i, j)); }
int main() {
  int n;
  cin >> n;
  vector<lint> a(n);
  rep(i, n) cin >> a[i];
  vector<lint> prime, d(1000010);
  for (int i = 2; i <= 1000000; i++) {
    bool is = true;
    for (int j = 0; j < prime.size(); j++) {
      if (i % prime[j] == 0) {
        is = false;
        d[i] = j;
        break;
      }
      if (prime[j] * prime[j] > i)
        break;
    }
    if (is) {
      prime.push_back(i);
      d[i] = prime.size() - 1;
    }
  }
  vector<bool> inc(prime.size(), false);
  bool ret1 = true, ret2 = true;
  rep(i, n) {
    vector<bool> each(prime.size(), false);
    int e = a[i];
    while (e > 1) {
      each[d[e]] = true;
      e %= prime[d[e]];
    }
    rep(j, prime.size()) {
      if (each[j] && inc[j])
        ret1 = false;
      else if (each[j])
        inc[j] = true;
    }
  }
  int gc = a[0];
  rep(i, n) gc = gcd(gc, a[i]);
  if (gc != 1)
    ret2 = false;
  if (ret1)
    cout << "pairwise coprime" << endl;
  else if (ret2)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}