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

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define rev2(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template <class T> using Vec = vector<T>;
template <class T> using VVec = vector<vector<T>>;
long long TEN(int i) { return !i ? 1 : TEN(i - 1) * 10LL; }
template <class T, class U> bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} iosetup;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int N;
int A[1000005];

void solve() {
  vector<int> table(1000005, -1);
  for (int i = 2; i < 1000005; i++) {
    for (int j = i; j < 1000005; j += i) {
      if (table[j] == -1) {
        table[j] = i;
      }
    }
  }
  vector<int> cnt(1000005, 0);
  rep(i, N) {
    int a = A[i];
    while (a != 1) {
      cnt[table[a]]++;
      int use = table[a];
      while (a % use == 0) {
        a = a / use;
      }
    }
  }

  if (*max_element(rng(cnt)) < 2) {
    cout << "pairwise coprime" << endl;
    return;
  }

  int _gcd = A[0];
  rep2(i, 1, N) _gcd = gcd(_gcd, A[i]);
  if (_gcd == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}

int main() {
  cin >> N;
  rep(i, N) cin >> A[i];

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define rev2(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template <class T> using Vec = vector<T>;
template <class T> using VVec = vector<vector<T>>;
long long TEN(int i) { return !i ? 1 : TEN(i - 1) * 10LL; }
template <class T, class U> bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} iosetup;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (ll 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);
}

int main() {
  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];

  vector<int> cnt(1000005, 0);
  rep(i, N) {
    vector<ll> tmp = divisor((ll)A[i]);
    rep2(j, 0, tmp.size()) cnt[tmp[j]]++;
  }

  sort(rrng(cnt));

  if (*max_element(cnt.begin() + 1, cnt.end()) < 2) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  if (*max_element(cnt.begin() + 1, cnt.end()) != N) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}