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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
// #define vb          vector<bool>
// #define vc          vector<char>
#define vii vector<pii>
// #define mi          map<int,int>
// #define mii         map<pii,int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
// #define ub(x)       upper_bound(x)
// #define lb(x)       lower_bound(x)
#define endl '\\n'
#define file                                                                   \\
  freopen("b1.in", "r", stdin);                                                \\
  freopen("output.txt", "w", stdout)
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// #define N            1e5+5
// #define M           1e9+7
#define MAX 50000
#define mem0(a) memset(a, 0, sizeof(a))
#define prec cout << std::setprecision(9) << std::fixed
#define forn(i, a, b) for (int i = a; i < b; i++)
#define pi 3.14159265358979323

struct cmp {
  bool operator()(pii a, pii b) {
    return ((a.F - a.F / 2) * a.S) < ((b.F - b.F / 2) * b.S);
  }
};

const int N = 1e6 + 1;
// int n,m,k;
int cnt[N], all;
const int MOD = 1e9 + 7;

void Anon_mouS() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[x]++;
    all = __gcd(all, x);
  }
  bool ok = true;
  for (int i = 2; i < N and ok; i++) {
    int sum = 0;
    for (int j = i; j < N; j += i)
      sum += cnt[j];
    if (sum > 1)
      ok = false;
  }
  if (ok)
    cout << "pairwise coprime" << endl;
  else {
    if (all == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  }
}
int32_t main() {
  fastio;
  int q = 1;
  // sieve();
  // cin>>q;
  for (int it = 1; it <= q; it++) {
    // cout<<"Case #"<<it<<": ";
    Anon_mouS();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define int         long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
// #define vb          vector<bool>
// #define vc          vector<char>
#define vii vector<pii>
// #define mi          map<int,int>
// #define mii         map<pii,int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
// #define sz(x)       (int)x.size()
// #define ub(x)       upper_bound(x)
// #define lb(x)       lower_bound(x)
#define endl '\\n'
#define file                                                                   \\
  freopen("b1.in", "r", stdin);                                                \\
  freopen("output.txt", "w", stdout)
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// #define N            1e5+5
#define PRIME 1000000007
#define MAX 50000
#define mem0(a) memset(a, 0, sizeof(a))
#define prec cout << std::setprecision(9) << std::fixed
#define forn(i, a, b) for (int i = a; i < b; i++)
#define pi 3.14159265358979323

// int n;
// const int N=1e6+5;
// vector<bool> prime(N);
// void sieve(){
//     for(int i=0;i<=N;i++)prime[i]=true;
//     prime[0]=prime[1]=false;
//     for(int i=2;i*i<=N;i++){
//         if(prime[i] and i*i<=N){
//             for(int j=i*i;j<=N;j+=i){
//                 prime[j]=false;
//             }
//         }
//     }
// }
// const int N=1e3+5;
// int a[N];

struct cmp {
  bool operator()(pii a, pii b) {
    return ((a.F - a.F / 2) * a.S) < ((b.F - b.F / 2) * b.S);
  }
};
// priority_queue<pii,vector<pii>,cmp> q;
const int N = 2e5 + 1;

void Anon_mouS() {
  int n;
  cin >> n;
  vi a(n);
  for (int &x : a)
    cin >> x;
  int g = a[0];
  for (int x : a) {
    g = __gcd(g, x);
  }
  sort(all(a));
  int cnt = 0;
  // bool ok=false;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (__gcd(a[i], a[j]) == 1) {
        cnt++;
      }
    }
  }
  if (cnt == (n * (n - 1) / 2))
    cout << "pairwise coprime" << endl;
  else if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}
int32_t main() {
  fastio;
  int q = 1;
  // sieve();
  // cin>>q;
  for (int it = 1; it <= q; it++) {
    // cout<<"Case #"<<it<<": ";
    Anon_mouS();
  }
  return 0;
}