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

#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
// fread->R
bool IOerror = 0;
// inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
  static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, BUF_SIZE, stdin);
    if (pend == p1) {
      IOerror = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool blank(char ch) {
  return ch == ' ' || ch == '\\n' || ch == '\\r' || ch == '\\t';
}
template <class T> inline bool R(T &x) {
  bool sign = 0;
  char ch = nc();
  x = 0;
  for (; blank(ch); ch = nc())
    ;
  if (IOerror)
    return false;
  if (ch == '-')
    sign = 1, ch = nc();
  for (; ch >= '0' && ch <= '9'; ch = nc())
    x = x * 10 + ch - '0';
  if (sign)
    x = -x;
  return true;
}
inline bool R(double &x) {
  bool sign = 0;
  char ch = nc();
  x = 0;
  for (; blank(ch); ch = nc())
    ;
  if (IOerror)
    return false;
  if (ch == '-')
    sign = 1, ch = nc();
  for (; ch >= '0' && ch <= '9'; ch = nc())
    x = x * 10 + ch - '0';
  if (ch == '.') {
    double tmp = 1;
    ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc())
      tmp /= 10.0, x += tmp * (ch - '0');
  }
  if (sign)
    x = -x;
  return true;
}
inline bool R(char *s) {
  char ch = nc();
  for (; blank(ch); ch = nc())
    ;
  if (IOerror)
    return false;
  for (; !blank(ch) && !IOerror; ch = nc())
    *s++ = ch;
  *s = 0;
  return true;
}
inline bool R(char &c) {
  c = nc();
  if (IOerror) {
    c = -1;
    return false;
  }
  return true;
}
template <class T, class... U> bool R(T &h, U &...t) { return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace fastIO
using namespace fastIO;
template <class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U> void _W(const pair<T, U> &x) {
  _W(x.F), putchar(' '), _W(x.S);
}
template <class T> void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin())
      putchar(' ');
}
void W() {}
template <class T, class... U> void W(const T &head, const U &...tail) {
  _W(head), putchar(sizeof...(tail) ? ' ' : '\\n'), W(tail...);
}
#pragma endregion
const int maxn = 1e6 + 5;
int a[maxn];
int pos[maxn];
vector<int> pr;
bool vis[maxn];
int cnt[maxn];
void findprime() {
  for (ll i = 2; i < maxn; ++i) {
    if (!vis[i])
      pr.push_back(i);
    for (ll j = i * i; j < maxn; j += i) {
      vis[j] = 1;
    }
  }
}
int main() {
  int n;
  R(n);
  rep(i, 1, n) R(a[i]);
  findprime();
  int f1 = 1, f2 = 1;
  rep(i, 1, n) {
    int t = a[i];
    for (auto it : pr) {
      if (t == 1)
        break;
      if (!vis[t]) {
        if (pos[t])
          f1 = 0;
        else
          pos[t] = i;
        cnt[t]++;
        t = 1;
        break;
      }
      if (t % it == 0) {
        if (pos[it])
          f1 = 0;
        else
          pos[it] = i;
        cnt[it]++;
        while (t % it == 0)
          t /= it;
      }
    }
  }
  rep(i, 2, 1e6) if (cnt[i] >= n) f2 = 0;
  if (f1)
    W("pairwise coprime");
  else if (f2)
    W("setwise coprime");
  else
    W("not coprime");
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
typedef long long ll;
#define lp(i, n) for (ll i = 0; i < ll(n); i++)
#define rlp(i, n) for (ll i = ll(n); i >= 0; i--)
#define rg(i, a, b) for (ll i = ll(a); i <= ll(b); i++)
#define wh(t) while (t)
#define re return
#define pb push_back
#define sz(s) (ll)(s.size())
#define fx(n) fixed << setprecision(n)
#define NeverGiveUp                                                            \\
  ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0),           \\
      cout.tie(0);
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
/*freopen("input.txt","r", stdin);
        freopen("output.txt","w", stdout);*/
using namespace std;
/*ll mx = INT_MIN;
ll mn = INT_MAX;*/
/*ll xd,dx[4]={1,0,-1,0};
ll yd,dy[4]={0,1,0,-1};
bool valid(ll xd,ll yd,ll n,ll m){return xd>-1&&xd<n&&yd>-1&&yd<m;}*/
const int sz = 2e6 + 5;
const ll md = 1e9 + 7;
// ll lcm(ll a,ll b){return a*b/__gcd(a,b);}
ll n, a[sz], g2, g;
bool f;
int main() {
  NeverGiveUp cin >> n;
  lp(i, n) cin >> a[i];
  lp(i, n) rg(j, i + 1, n - 1) {
    g = __gcd(a[i], a[j]);
    if (g != 1) {
      f = 1;
      break;
    }
  }
  lp(i, n) { g2 = __gcd(g2, a[i]); }
  if (!f)
    cout << "pairwise coprime";
  else if (g2 == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  re 0;
}