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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
typedef pair<int, int> P;
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void coY() { cout << "Yes" << endl; }
void coN() { cout << "No" << endl; }

// Write From this Line

// const ll mod = 1e9+7;
// const ll mod = 998244353;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  SORT(a);
  vector<ll> check(1000005, 0);
  int flag = 1;
  int nflag = 1;
  vector<ll> done(1000005, 0);
  rep(i, n) {
    // pairwise じゃないならflag を 0にする
    //  a[i]の素因数の倍数を表から消す
    ll now = a[i];
    if (check[now] >= 1)
      flag = 0;
    for (int j = 2; j * j < now; j++) {
      // ルートnまでが素因数かどうか
      if (now % j == 0) {
        if (done[j]) {
          break;
        } else {
          now /= j;
          done[j] = 1;
          for (int k = j; k < 1000005; k += j) {
            check[k]++;
          }
        }
      }
    }
    // now が1じゃなければ同じことをやる
    if (now != 1) {
      if (done[now])
        continue;
      else {
        for (int j = now; j < 1000005; j += now) {
          check[j]++;
        }
      }
    }
  }
  // 全部のgcdが1か見ればいい
  ll G = 0;
  rep(i, n) { G = __gcd(G, a[i]); }
  if (flag == 1) {
    cout << "pairwise coprime" << endl;
  } else if (G == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
typedef pair<int, int> P;
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void coY() { cout << "Yes" << endl; }
void coN() { cout << "No" << endl; }

// Write From this Line

// const ll mod = 1e9+7;
// const ll mod = 998244353;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  SORT(a);
  vector<ll> check(1000005, 0);
  int flag = 1;
  int nflag = 1;
  vector<ll> done(1000005, 0);
  rep(i, n) {
    // pairwise じゃないならflag を 0にする
    //  a[i]の素因数の倍数を表から消す
    ll now = a[i];
    if (check[now] >= 1)
      flag = 0;
    for (int j = 2; j * j < now; j++) {
      // ルートnまでが素因数かどうか
      if (now % j == 0) {
        if (done[j]) {
          now /= j;
          continue;
        } else {
          now /= j;
          done[j] = 1;
          for (int k = j; k < 1000005; k += j) {
            check[k]++;
          }
        }
      }
    }
    // now が1じゃなければ同じことをやる
    if (now != 1) {
      for (int j = now; j < 1000005; j += now) {
        check[j]++;
      }
    }
  }
  // 全部のgcdが1か見ればいい
  ll G = a[0];
  rep(i, n) { G = __gcd(G, a[i]); }
  if (flag == 1) {
    cout << "pairwise coprime" << endl;
  } else if (G == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}