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

// @iamnifer
#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define ld long double
#define ull unsigned long long
#define elif else if
#define inf (ll)1e18
#define append push_back
#define pb push_back
#define str to_string
#define gcd __gcd
#define optimise                                                               \\
  cout.tie(0);                                                                 \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0);
#define all(x) x.begin(), x.end()
#define rall(x) r.rbegin(), x.rend()
#define len(x) (ll) x.size()
#define add insert
#define PI 3.14159265358979323846264338327950288419716939937510
#define lcm(a, b) a / gcd(a, b) * b
#define eps 0.0000000001
#define endl '\\n'
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
// #pragma comment(linker,"/STACK:128777216")
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
using namespace std;
typedef vector<vector<ll>> graph;
const ll MOD = (ll)1e9 + 7; // 998244353
const ll maxN = (ll)1e6 + 1;
vector<ll> p;
vector<ll> sieve(maxN, true);
void getprimes() {
  for (ll i = 2; i < maxN; i++) {
    if (sieve[i]) {
      for (ll j = i << 1; j < maxN; j += i) {
        sieve[j] = false;
      }
    }
  }
  for (ll i = 2; i < maxN; i++) {
    if (sieve[i]) {
      p.pb(i);
    }
  }
}
int main() {
  optimise ll n;
  cin >> n;
  ll s[n];
  for (auto &i : s)
    cin >> i;
  getprimes();
  set<ll> facts;
  ll g = 0;
  bool pairwise = true;
  for (auto &i : s) {
    g = gcd(g, i);
    if (!pairwise)
      continue;
    for (auto &pr : p) {
      if (pr * pr > i)
        break;
      if (i % pr == 0) {
        if (facts.count(pr)) {
          pairwise = false;
          break;
        }
        facts.add(pr);
        while (i % pr == 0)
          i /= pr;
      }
    }
    if (i > 1) {
      if (facts.count(i))
        pairwise = false;
      facts.add(i);
    }
  }
  if (pairwise)
    cout << "pairwise coprime";
  else if (g == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}

Error: TLE

Fixed submission:

// @iamnifer
#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define ld long double
#define ull unsigned long long
#define elif else if
#define inf (ll)1e18
#define append push_back
#define pb push_back
#define str to_string
#define gcd __gcd
#define optimise                                                               \\
  cout.tie(0);                                                                 \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0);
#define all(x) x.begin(), x.end()
#define rall(x) r.rbegin(), x.rend()
#define len(x) (ll) x.size()
#define add insert
#define PI 3.14159265358979323846264338327950288419716939937510
#define lcm(a, b) a / gcd(a, b) * b
#define eps 0.0000000001
#define endl '\\n'
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
// #pragma comment(linker,"/STACK:128777216")
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
using namespace std;
typedef vector<vector<ll>> graph;
const ll MOD = (ll)1e9 + 7; // 998244353
const ll maxN = (ll)1e6 + 1;
vector<ll> p;
vector<ll> sieve(maxN, true);
void getprimes() {
  for (ll i = 2; i < maxN; i++) {
    if (sieve[i]) {
      for (ll j = i << 1; j < maxN; j += i) {
        sieve[j] = false;
      }
    }
  }
  for (ll i = 2; i < maxN; i++) {
    if (sieve[i]) {
      p.pb(i);
    }
  }
}
int main() {
  optimise ll n;
  cin >> n;
  ll s[n];
  for (auto &i : s)
    cin >> i;
  getprimes();
  set<ll> facts;
  ll g = 0;
  bool pairwise = true;
  for (auto &i : s) {
    g = gcd(g, i);
    if (pairwise)
      for (auto &pr : p) {
        if (i % pr == 0) {
          if (facts.count(pr))
            pairwise = false;
          facts.add(pr);
        }
      }
  }
  if (pairwise)
    cout << "pairwise coprime";
  else if (g == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}