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

// vec[i]の範囲外エラー表示
#define _GLIBCXX_DEBUG
// includeとusing
#include <bits/stdc++.h>
using namespace std;
// 型名省略
using ll = long long;
using st = string;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vs = vector<string>;
using pll = pair<ll, ll>;
// 定数
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
// マクロ
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define rrep(i, n) for (ll i = (ll)n - 1; i >= 0; i--)
#define REP(i, x, n) for (ll i = (ll)x; i < (ll)n; i++)
#define RREP(i, x, n) for (ll i = (ll)n - 1; i >= x; i--)
#define each(i, A) for (ll i : (vl)A)
#define all(A) A.begin(), A.end()
#define Size(A) ll(A.size())
// 入力
template <typename T> T input() {
  T x;
  cin >> x;
  return x;
}
// 正誤判定
void Yes(bool ans) { cout << (ans ? "Yes" : "No") << endl; }
void YES(bool ans) { cout << (ans ? "YES" : "NO") << endl; }
// 余り切り上げ
ll ceil(ll a, ll b) { return (a + b - 1) / b; }
// 最大値・最小値
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// 最大公約数
ll gcd(ll a, ll b) { /*if(a<b)swap(a,b);*/
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// n乗の余り
ll modPow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modPow(x * x, n / 2, mod) % mod;
  return x % mod * modPow(x, n - 1, mod) % mod;
}

/*------------------------------------------------*/

// main関数
int main() {
  ll n;
  cin >> n;

  vl a(n), count(1e6 + 1);
  ll GCD = 0;
  rep(i, n) {
    cin >> a[i];
    count[a[i]]++;
    GCD = gcd(GCD, a[i]);
  }

  if (GCD != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  REP(i, 2, 1e6) {
    ll div_count = count[i];
    ll now = 2 * i;
    while (now <= 1e6) {
      div_count += count[now];
      now += i;
    }
    if (div_count >= 2) {
      cout << "setwise coprime" << endl;
      return 0;
    }
  }

  cout << "pairwise coprime" << endl;
}


Error: TLE

Fixed submission:

// vec[i]の範囲外エラー表示
#define _GLIBCXX_DEBUG
// includeとusing
#include <bits/stdc++.h>
using namespace std;
// 型名省略
using ll = long long;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vs = vector<string>;
using pll = pair<ll, ll>;
// 定数
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
// マクロ
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define xrep(i, x, n) for (ll i = (ll)x; i < (ll)n; i++)
#define all(A) A.begin(), A.end()
// 入力
ll llin() {
  ll x;
  cin >> x;
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
vl vlin(ll n) {
  vl a(n);
  rep(i, n) { cin >> a[i]; }
  return a;
}
// 正誤判定
void Yes(bool ans) { cout << (ans ? "Yes" : "No") << endl; }
void YES(bool ans) { cout << (ans ? "YES" : "NO") << endl; }
// 最大公約数
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

// エラトステネスの篩
vl Eratosthenes(ll n) {
  vector<bool> is_prime(n + 1, true);
  vl P;
  xrep(i, 2, n + 1) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}

// main関数
int main() {
  ll n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];

  sort(all(a));

  bool pc = true;
  bool sc = true;
  for (ll i : Eratosthenes(a[n - 1])) {
    ll dividecount = 0;
    rep(j, n) {
      if (a[j] % i == 0) {
        dividecount++;
      }
    }
    if (dividecount >= 2)
      pc = false;
    if (dividecount == n)
      sc = false;
    if (!pc && !sc)
      break;
  }
  if (pc)
    cout << "pairwise coprime" << endl;
  else if (sc)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}