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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPS(i, n) for (int i = 1; i <= (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for (int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back

using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  vi A(N);
  REP(i, N) cin >> A[i];
  vi B(A);

  bool pairwise = true;

  vb used(1000010, false);

  REP(i, N) {
    for (int j = 2; j * j <= A[i]; j++) {
      if (A[i] % j != 0)
        continue;
      if (used[j]) {
        pairwise = false;
        break;
      } else {
        used[j] = true;
        while (A[i] % j == 0)
          A[i] /= j;
      }
    }
    if (A[i] != 1) {
      if (used[A[i]])
        pairwise = false;
      else
        used[A[i]] = true;
    }
    if (!pairwise)
      break;
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int x = B[0];
  REP(i, N - 1) x = gcd(x, B[i + 1]);
  if (x == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPS(i, n) for (int i = 1; i <= (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for (int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back

using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  vb isPrime(1000010, true);
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i <= 1000010; i++) {
    if (isPrime[i]) {
      for (int j = 2 * i; j < 1000010; j += i)
        isPrime[j] = false;
    }
  }

  vi prime;
  REP(i, (int)isPrime.size()) if (isPrime[i]) prime.pb(i);

  int N;
  cin >> N;
  vi A(N);
  REP(i, N) cin >> A[i];

  bool pairwise = true;

  vb used((int)prime.size(), false);

  REP(i, N) {
    REP(j, (int)prime.size()) {
      if ((A[i] % prime[j] == 0) && used[j])
        pairwise = false;
      else if (A[i] % prime[j] == 0)
        used[j] = true;
    }
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int x = A[0];
  REP(i, N - 1) x = gcd(x, A[i + 1]);
  if (x == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}