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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define pr(s) cout << (s) << '\\n'
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

vector<ll> prime;

void setPrime(ll x) {
  for (ll i = 2; i <= x; i++) {
    bool ok = true;
    for (ll e : prime) {
      if (i % e == 0) {
        ok = false;
        break;
      }
      if (e * e > i) {
        break;
      }
    }
    if (ok)
      prime.push_back(i);
  }
}

const int NMAX = 1000005;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  setPrime(NMAX);
  vi a(n);

  rep(i, n) cin >> a[i];
  int total = a[0];
  rep(i, n) { total = __gcd(total, a[i]); }
  if (total != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  vector<bool> v(NMAX);
  bool ok = true;
  rep(i, n) {
    int now = a[i];
    for (int e : prime) {
      if (e * e > now) {
        if (now > 1) {
          if (v[now]) {
            ok = false;
            break;
          }
          v[now] = true;
        }
        break;
      }
      if (now % e == 0) {
        while (now % e == 0) {
          now /= e;
        }
        if (v[e]) {
          ok = false;
          break;
        }
        v[e] = true;
      }
    }

    if (!ok)
      break;
  }
  if (ok)
    cout << "pairwise coprime" << endl;
  else
    cout << "setwise coprime" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define pr(s) cout << (s) << '\\n'
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

vector<ll> prime;

void setPrime(ll x) {
  for (ll i = 2; i <= x; i++) {
    bool ok = true;
    for (ll e : prime) {
      if (i % e == 0) {
        ok = false;
        break;
      }
      if (e * e > i) {
        break;
      }
    }
    if (ok)
      prime.push_back(i);
  }
}

const int NMAX = 1000005;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  setPrime(NMAX);
  vi a(n);

  rep(i, n) cin >> a[i];
  int total = a[0];
  rep(i, n) { total = __gcd(total, a[i]); }
  if (total != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  vector<bool> v(NMAX);
  bool ok = true;
  rep(i, n) {
    int now = a[i];
    for (int e : prime) {
      if (now % e == 0) {
        now /= e;
        if (v[e]) {
          ok = false;
          break;
        }
        v[e] = true;
      }
      if (e > now)
        break;
    }
    if (!ok)
      break;
  }
  if (ok)
    cout << "pairwise coprime" << endl;
  else
    cout << "setwise coprime" << endl;
  return 0;
}