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

// Bismillahir Rahmanir Raheem
// #include<bits/stdc++.h>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
#define FF first
#define SS second
#define MP make_pair
#define mxN (int)2e5 + 2
#define sz(n) (int)(n).size()
#define fasterIO                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
ll gcd(ll a, ll b) {
  if (b == 0LL)
    return a;
  return gcd(b, a % b);
}
ll bigmod(ll a, ll b, ll mod) {
  if (b == 0LL)
    return 1LL;
  ll sq = bigmod(a, b / 2LL, mod);
  sq = (sq * sq) % mod;
  if (b & 1LL)
    return (sq * (a % mod)) % mod;
  return sq;
}
#define SIZE (int)(1e6 + 5)
#define INF (int)1e9
bool prime[SIZE + 1];
vector<int> primes;

int main() {
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= SIZE; p++) {
    if (prime[p]) {
      for (int i = p * p; i <= SIZE; i += p)
        prime[i] = false;
    }
  }
  for (int i = 2; i <= SIZE; i++)
    if (prime[i])
      primes.pb(i);
  fasterIO int n;
  cin >> n;
  vi a(n);
  map<int, bool> used;
  bool pp = 1;
  for (auto &j : a) {
    cin >> j;
    int x = j;
    for (int i = 0; primes[i] * primes[i] <= x; i++) {
      int p = primes[i];
      bool flag = 0;
      while (x % p == 0) {
        x /= p;
        flag = 1;
      }
      if (flag) {
        if (!used[p])
          used[p] = 1;
        else {
          pp = 0;
          break;
        }
      }
    }
    if (x > 2) {
      if (!used[x])
        used[x] = 1;
      else {
        pp = 0;
        break;
      }
    }
  }
  int gc = 0;
  for (auto x : a)
    gc = gcd(x, gc);
  if (gc > 1)
    cout << "not coprime\\n";
  else {
    if (pp)
      cout << "pairwise coprime\\n";
    else
      cout << "setwise coprime\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

// Bismillahir Rahmanir Raheem
// #include<bits/stdc++.h>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
#define FF first
#define SS second
#define MP make_pair
#define mxN (int)2e5 + 2
#define sz(n) (int)(n).size()
#define fasterIO                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
ll gcd(ll a, ll b) {
  if (b == 0LL)
    return a;
  return gcd(b, a % b);
}
ll bigmod(ll a, ll b, ll mod) {
  if (b == 0LL)
    return 1LL;
  ll sq = bigmod(a, b / 2LL, mod);
  sq = (sq * sq) % mod;
  if (b & 1LL)
    return (sq * (a % mod)) % mod;
  return sq;
}
#define SIZE (int)(1e6 + 5)
#define INF (int)1e9
bool prime[SIZE + 1];
vector<int> primes;

int main() {
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= SIZE; p++) {
    if (prime[p]) {
      for (int i = p * p; i <= SIZE; i += p)
        prime[i] = false;
    }
    for (int i = 2; i <= SIZE; i++)
      if (prime[i])
        primes.pb(i);
  }
  fasterIO int n;
  cin >> n;
  vi a(n);
  map<int, bool> used;
  bool pp = 1;
  for (auto &j : a) {
    cin >> j;
    int x = j;
    for (int i = 0; primes[i] * primes[i] <= x; i++) {
      int p = primes[i];
      bool flag = 0;
      while (x % p == 0) {
        x /= p;
        flag = 1;
      }
      if (flag) {
        if (!used[p])
          used[p] = 1;
        else {
          pp = 0;
          break;
        }
      }
    }
    if (x > 2) {
      if (!used[x])
        used[x] = 1;
      else {
        pp = 0;
        break;
      }
    }
  }
  int gc = 0;
  for (auto x : a)
    gc = gcd(x, gc);
  if (gc > 1)
    cout << "not coprime\\n";
  else {
    if (pp)
      cout << "pairwise coprime\\n";
    else
      cout << "setwise coprime\\n";
  }

  return 0;
}