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

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0)
// #define MULTI_TEST
// #define GOOGLE
#ifdef LOCAL
#include "debugger.h"
#else
#define db(...)
#endif
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define s32 set<int>
#define s64 set<ll>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define sz(v) (ll)(v).size()
#define fi first
#define se second
#define ln '\\n'
const ll MOD = 1e9 + 7;
inline ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
inline ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }

const int N = 1e6;

void solve(int tc) {
  int n;
  cin >> n;
  v32 arr(n, 0);
  multiset<int> bst;
  int g = 0;
  for (int i = 0; i < n; i++)
    cin >> arr[i], g = __gcd(g, arr[i]), bst.insert(arr[i]);
  bool pairwise = true;
  bool setwise = (g == 1);
  int mx = *max_element(arr.begin(), arr.end());
  vector<int> ctr(mx + 1, 0);
  for (int i = 2; i < mx + 1; i++) {
    if (ctr[i])
      continue;
    int cnt = 0;
    for (int j = i; j < mx + 1; j += i) {
      cnt += bst.count(j);
      ctr[j]++;
      if (cnt > 1) {
        pairwise = false;
        break;
      }
    }
  }
  if (pairwise)
    cout << "pairwise coprime" << ln;
  else if (!pairwise && setwise)
    cout << "setwise coprime" << ln;
  else
    cout << "not coprime" << ln;
  return;
}

int main() {
#ifndef LOCAL
  FASTIO;
#endif
  int t = 1;
#ifdef LOCAL
  clock_t start, stop;
  start = clock();
#endif
#ifdef MULTI_TEST
  cin >> t;
#endif
  for (int tc = 1; tc < t + 1; tc++) {
#ifdef GOOGLE
    cout << "Case #" << tc << ": ";
#endif
    solve(tc);
  }
#ifdef LOCAL
  stop = clock();
  cerr << "Time :" << fixed << setprecision(5)
       << double(stop - start) / double(CLOCKS_PER_SEC) << "s" << ln;
#endif
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0)
// #define MULTI_TEST
// #define GOOGLE
#ifdef LOCAL
#include "debugger.h"
#else
#define db(...)
#endif
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define s32 set<int>
#define s64 set<ll>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define sz(v) (ll)(v).size()
#define fi first
#define se second
#define ln '\\n'
const ll MOD = 1e9 + 7;
inline ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
inline ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }

const int N = 1e6;

void solve(int tc) {
  int n;
  cin >> n;
  v32 arr(n, 0);
  multiset<int> bst;
  int g = 0;
  for (int i = 0; i < n; i++)
    cin >> arr[i], g = __gcd(g, arr[i]), bst.insert(arr[i]);
  bool pairwise = true;
  bool setwise = (g == 1);
  int mx = *max_element(arr.begin(), arr.end());
  for (int i = mx; i > 1; i--) {
    int cnt = 0;
    for (int j = i; j < mx + 1; j += i) {
      cnt += bst.count(j);
      if (cnt > 1) {
        pairwise = false;
        break;
      }
    }
  }
  if (pairwise)
    cout << "pairwise coprime" << ln;
  else if (!pairwise && setwise)
    cout << "setwise coprime" << ln;
  else
    cout << "not coprime" << ln;
  return;
}

int main() {
#ifndef LOCAL
  FASTIO;
#endif
  int t = 1;
#ifdef LOCAL
  clock_t start, stop;
  start = clock();
#endif
#ifdef MULTI_TEST
  cin >> t;
#endif
  for (int tc = 1; tc < t + 1; tc++) {
#ifdef GOOGLE
    cout << "Case #" << tc << ": ";
#endif
    solve(tc);
  }
#ifdef LOCAL
  stop = clock();
  cerr << "Time :" << fixed << setprecision(5)
       << double(stop - start) / double(CLOCKS_PER_SEC) << "s" << ln;
#endif
  return 0;
}