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

/*
    author:ujwalll
*/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
const int MOD = 1073741824;
#define pp pair<ll, ll>
#define ppi pair<int, int>
typedef long long int ll;
bool isPowerOfTwo(ll x) { return x && (!(x & (x - 1))); }
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
////////////////////////////////////////////////////////////////////
int freq[1000005];
// vector<int> v[100005];
bool solve(int a[], int n) {
  rep(j, n) {
    int x = a[j];
    for (int i = 2; i * i <= x; i++) {
      if (x % i == 0)
        freq[i]++;
      if (freq[i] > 1)
        return 0;
      while (x % i == 0)
        x /= i;
    }
    if (x > 1)
      freq[x]++;
    if (freq[x] > 1)
      return 0;
  }
  return 1;
}
int main() {
  int tc = 1;
  // cin>>tc;
  while (tc--) {
    int n;
    cin >> n;
    int a[n];
    int gc = 0;
    rep(i, n) {
      cin >> a[i];
      gc = __gcd(gc, a[i]);
    }
    if (solve(a, n))
      cout << "pairwise coprime";
    else if (gc == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
    author:ujwalll
*/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
const int MOD = 1073741824;
#define pp pair<ll, ll>
#define ppi pair<int, int>
typedef long long int ll;
bool isPowerOfTwo(ll x) { return x && (!(x & (x - 1))); }
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
////////////////////////////////////////////////////////////////////
int freq[1000005];
// vector<int> v[100005];
bool solve(int a[], int n) {
  rep(j, n) {
    int x = a[j];
    for (int i = 2; i <= 1000000; i++) {
      if (x % i == 0)
        freq[i]++;
      if (freq[i] > 1)
        return 0;
      while (x % i == 0)
        x /= i;
    }
    if (x > 1)
      freq[x]++;
  }
  return 1;
}
int main() {
  int tc = 1;
  // cin>>tc;
  while (tc--) {
    int n;
    cin >> n;
    int a[n];
    int gc = 0;
    rep(i, n) {
      cin >> a[i];
      gc = __gcd(gc, a[i]);
    }
    if (solve(a, n))
      cout << "pairwise coprime";
    else if (gc == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
  return 0;
}