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

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#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())
using Pair = pair<ll, pair<int, int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod = 1000000007;
// const ll mod=998244353;
const ld pi = acos(-1.0);
const ll INF = 1LL << 61;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

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

  int n;
  cin >> n;

  vector<ll> p(n);
  rep(i, n) { cin >> p[i]; }

  ll allgcd = p[0];
  repo(i, n) { allgcd = gcd(allgcd, p[i]); }

  if (allgcd != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  vector<ll> q(1010101);
  rep(i, n) {
    for (int j = 1; j * j <= p[i]; j++) {
      if (p[i] % j != 0)
        continue;
      int a = j;
      int b = p[i] / j;
      bool asosu = true;
      if (a == 1)
        asosu = false;
      for (int k = 2; k * k <= a; k++) {
        if (a % k == 0) {
          asosu = false;
          break;
        }
      }
      if (asosu) {
        q[a]++;
        if (q[a] >= 2) {
          cout << "setwise coprime" << endl;
          return 0;
        }
      }

      if (b == a)
        continue;
      bool bsosu = true;
      for (int k = 2; k * k <= b; k++) {
        if (b % k == 0) {
          bsosu = false;
          break;
        }
      }
      if (bsosu) {
        q[b]++;
        if (q[b] >= 2) {
          cout << "setwise coprime" << endl;
          return 0;
        }
      }
    }
  }

  cout << "pairwise coprime" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#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())
using Pair = pair<ll, pair<int, int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod = 1000000007;
// const ll mod=998244353;
const ld pi = acos(-1.0);
const ll INF = 1LL << 61;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

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

  int n;
  cin >> n;

  vector<ll> p(n);
  rep(i, n) { cin >> p[i]; }

  ll allgcd = p[0];
  repo(i, n) { allgcd = gcd(allgcd, p[i]); }

  if (allgcd != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  vector<ll> so;
  for (int i = 2; i <= 1000000; i++) {
    bool sosu = true;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        sosu = false;
        break;
      }
    }
    if (sosu)
      so.pb(i);
  }

  int ss = so.size();
  vector<ll> q(ss);
  rep(i, n) {
    rep(j, ss) {
      if (p[i] < so[j])
        break;
      if (p[i] % so[j] == 0) {
        q[j]++;
        if (q[j] >= 2) {
          cout << "setwise coprime" << endl;
          return 0;
        }
      }
    }
  }

  cout << "pairwise coprime" << endl;
}