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

#include <bits/stdc++.h>
#define _for(i, a, b) for (int i = (a); i < (b); i++)
#define _count(i, a, b) for (int i = (a); i <= b; i++)
#define PB emplace_back
#define MINI -2147483648
#define MAXI 2147483647
#define MAXLL 9223372036854775807
#define MINLL -9223372036854775808
#define VI vector<int>
#define VB vector<bool>
#define VLL vector<long long>
#define LL long long
#define ULL unsigned long long
#define ALL(X) X.begin(), X.end()
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
// #define Remilia_is_so_cute
#define StarBurst std::ios::sync_with_stdio(false), cin.tie(NULL);
#define lowerBit(x) x & -x
using namespace std;

inline void R_P(vector<int> &vec) {
  int temp;
  cin >> temp;
  vec.push_back(temp);
}
inline bool checkMax(int &base, int test) {
  return (test > base) ? base = test, true : false;
}

inline bool checkMin(int &base, int test) {
  return (test < base) ? base = test, true : false;
}
template <class T> void test(vector<T> &vec, const string &warn) {
  cout << warn << '\\n';
  _for(i, 0, vec.size()) cout << vec[i] << ' ';
  cout << "\\nTest end..............\\n";
}

int n;
bool prime[1501];
VI primes;
/*
int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  while(a != 0 && b != 0) {
    a %= b;
    if (a != 0)
      b %= a;
  }

  return a + b;
}*/
int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  else
    return gcd(a % b, b);
}
int main() {
  StarBurst
#ifdef Remilia_is_so_cute
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  _for(i, 2, 1501) prime[i] = true;
  _for(i, 2, 1501) {
    if (!prime[i])
      continue;

    primes.PB(i);
    int r = 1;
    while (++r * i <= 1500)
      prime[i * r] = false;
  }

  cin >> n;
  VI vec(n, 0);
  _for(i, 0, n) cin >> vec[i];

  set<int> se;
  bool ok = true;
  for (int X : vec) {

    for (int pri : primes) {

      if (X % pri == 0) {
        while (X % pri == 0)
          X /= pri;
        if (se.count(pri))
          ok = false;
        se.insert(pri);
      }
      if (X == 1)
        break;
    }

    if (X != 1) {
      if (se.count(X))
        ok = false;
      se.insert(X);
    }
  }
  // cout << "test\\n";
  /*
  for(int X : se) {
    cout << X << '\\n';
  }*/
  if (ok) {
    cout << "pairwise coprime\\n";
    return 0;
  }

  int tmp = gcd(vec[0], vec[1]);
  _for(i, 2, n) tmp = gcd(tmp, vec[i]);

  if (tmp == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _for(i, a, b) for (int i = (a); i < (b); i++)
#define _count(i, a, b) for (int i = (a); i <= b; i++)
#define PB emplace_back
#define MINI -2147483648
#define MAXI 2147483647
#define MAXLL 9223372036854775807
#define MINLL -9223372036854775808
#define VI vector<int>
#define VB vector<bool>
#define VLL vector<long long>
#define LL long long
#define ULL unsigned long long
#define ALL(X) X.begin(), X.end()
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
// #define Remilia_is_so_cute
#define StarBurst std::ios::sync_with_stdio(false), cin.tie(NULL);
#define lowerBit(x) x & -x
using namespace std;

inline void R_P(vector<int> &vec) {
  int temp;
  cin >> temp;
  vec.push_back(temp);
}
inline bool checkMax(int &base, int test) {
  return (test > base) ? base = test, true : false;
}

inline bool checkMin(int &base, int test) {
  return (test < base) ? base = test, true : false;
}
template <class T> void test(vector<T> &vec, const string &warn) {
  cout << warn << '\\n';
  _for(i, 0, vec.size()) cout << vec[i] << ' ';
  cout << "\\nTest end..............\\n";
}

int n;
bool prime[1000001];
VI primes;
int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  while (a != 0 && b != 0) {
    a %= b;
    if (a != 0)
      b %= a;
  }

  return a + b;
}
int main() {
  StarBurst
#ifdef Remilia_is_so_cute
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  _for(i, 2, 1000001) prime[i] = true;
  _for(i, 2, 1000001) {
    if (!prime[i])
      continue;

    primes.PB(i);
    int r = 1;
    while (++r * i <= 1000000)
      prime[i * r] = false;
  }

  cin >> n;
  VI vec(n, 0);
  _for(i, 0, n) cin >> vec[i];

  set<int> se;
  bool ok = true;
  for (int X : vec) {

    for (int pri : primes) {

      if (X % pri == 0) {
        if (se.count(pri))
          ok = false;
        se.insert(pri);
      }
    }
  }

  if (ok) {
    cout << "pairwise coprime\\n";
    return 0;
  }

  int tmp = gcd(vec[0], vec[1]);
  _for(i, 2, n) tmp = gcd(tmp, vec[2]);

  if (tmp == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }

  return 0;
}