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

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef unsigned long long _ulong;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define endk '\\n'
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const lint MOD = 1e9 + 7, INF = 1e18;
lint dx[8] = {0, -1, 1, 0, 1, -1, 1, -1}, dy[8] = {1, 0, 0, -1, -1, -1, 1, 1};
typedef pair<double, lint> Pa;
typedef pair<plint, lint> tlint;
struct edge {
  lint cost;
  lint u, v;
};

vector<int64_t> divisor(int64_t n) {
  vector<int64_t> ret;
  for (int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

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

const lint MAX_A = 1000000;
lint N, arr[MAX_A];
bool tbl[MAX_A + 1];
int main() {
  cin >> N;
  REP(i, N) { cin >> arr[i]; }
  bool flag = true;
  REP(i, MAX_A + 1) tbl[i] = true;
  REP(i, N) {
    auto vec = divisor(arr[i]);
    REP(j, SZ(vec) - 1) {
      if (!tbl[vec[j + 1]]) {
        flag = false;
        break;
      }
      tbl[vec[j + 1]] = false;
    }
    if (!flag)
      break;
  }
  if (flag) {
    cout << "pairwise coprime" << endk;
    return 0;
  }

  lint _gcd = arr[0];
  REP(i, N - 1) { _gcd = gcd(arr[i + 1], _gcd); }
  if (_gcd == 1) {
    cout << "setwise coprime" << endk;
  } else {
    cout << "not coprime" << endk;
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef unsigned long long _ulong;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define endk '\\n'
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const lint MOD = 1e9 + 7, INF = 1e18;
lint dx[8] = {0, -1, 1, 0, 1, -1, 1, -1}, dy[8] = {1, 0, 0, -1, -1, -1, 1, 1};
typedef pair<double, lint> Pa;
typedef pair<plint, lint> tlint;
struct edge {
  lint cost;
  lint u, v;
};

vector<bool> prime_table(int n) {
  vector<bool> prime(n + 1, true);
  if (n >= 0)
    prime[0] = false;
  if (n >= 1)
    prime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i) {
      prime[j] = false;
    }
  }
  return prime;
}

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

const lint MAX_A = 1000000;
lint N, arr[MAX_A];
int main() {
  cin >> N;
  REP(i, N) { cin >> arr[i]; }
  auto vec = prime_table(MAX_A + 1);
  bool flag = true;
  REP(i, MAX_A + 1) {
    lint cnt = 0;
    if (!vec[i])
      continue;
    REP(j, N) {
      if (arr[j] % i == 0)
        cnt++;
    }
    if (cnt > 1) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "pairwise coprime" << endk;
    return 0;
  }

  lint _gcd = arr[0];
  REP(i, N - 1) { _gcd = gcd(arr[i + 1], _gcd); }
  if (_gcd == 1) {
    cout << "setwise coprime" << endk;
  } else {
    cout << "not coprime" << endk;
  }
}