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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Main {
  int N;
  vector<ll> A;
  ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
  void input() {
    cin >> N;
    A.resize(N);
    REP(i, N) { cin >> A[i]; }
  }
  void output() {
    ll g = A[0];
    REP(i, N - 1) { g = gcd(g, A[i + 1]); }
    if (g > 1) {
      cout << "not coprime" << endl;
      return;
    }
    vector<ll> prime;
    int a = 1000;
    vector<int> memo(a);
    FOR(i, 2, a) {
      if (memo[i])
        continue;
      prime.emplace_back(i);
      for (ll j = i; i * j < a; ++j) {
        memo[i * j] = 1;
      }
    }
    set<ll> st;
    REP(i, N) {
      for (auto &p : prime) {
        if (A[i] % p)
          continue;
        if (st.count(p)) {
          cout << "setwise coprime" << endl;
          return;
        }
        st.emplace(p);
        while (A[i] % p == 0)
          A[i] /= p;
        if (A[i] == 1)
          break;
      }
      if (A[i] > 1) {
        if (st.count(A[i])) {
          cout << "setwise coprime" << endl;
          return;
        }
        st.emplace(A[i]);
      }
    }
    cout << "pairwise coprime" << endl;
  }

public:
  Main() {
    input();
    output();
  }
};

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

  Main();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Main {
  int N;
  vector<ll> A;
  ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
  void input() {
    cin >> N;
    A.resize(N);
    REP(i, N) { cin >> A[i]; }
  }
  void output() {
    ll g = A[0];
    REP(i, N - 1) { g = gcd(g, A[i + 1]); }
    if (g > 1) {
      cout << "not coprime" << endl;
      return;
    }
    vector<ll> prime;
    int a = 1000000;
    vector<int> memo(a);
    FOR(i, 2, a) {
      if (memo[i])
        continue;
      prime.emplace_back(i);
      for (ll j = i; i * j < a; ++j) {
        memo[i * j] = 1;
      }
    }
    for (auto &p : prime) {
      int cnt = 0;
      REP(i, N) { cnt += A[i] % p == 0; }
      if (cnt > 1) {
        cout << "setwise coprime" << endl;
        return;
      }
    }
    cout << "pairwise coprime" << endl;
  }

public:
  Main() {
    input();
    output();
  }
};

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

  Main();

  return 0;
}