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

// 😄😄😄😄😄😄😄😄😄😄😄😄😄
// Apno Swagat che//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pi pair<int, int>
#define vp vector<pi>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define For(i, a, b) for (ll i = a; i < b; i++)
#define endl "\\n"
#define debug(x) cout << "AA Baju Smit------> " << #x << " -> " << x << endl
#define all(x) x.begin(), x.end()
#define mint map<int, int>
#define mod 10000007
#define ciN cin
#define X INT_MAX
#define N INT_MIN
////////////////////////////////////////////////////////////////
// Ha Moj Ha///
////////////////////////////////////////////////////////////////
////TEST CASES ATTACHED CHE BHULTO NAI......///////////////////
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
vector<bool> Sieve(int n) {

  vector<bool> is_prime(n + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i] && 1LL * i * i <= n) {
      for (int j = i * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return is_prime;
}

void jabru() {
  vector<bool> p = Sieve(1000000);
  vi xx;
  for (int i = 1; i <= 1000000; i++) {
    if (p[i])
      xx.pb(i);
  }
  int n;
  ciN >> n;
  vi v(n);
  For(i, 0, n) cin >> v[i];
  int gcc = v[0];
  mint has;
  bool is_pos = 1;
  For(i, 0, n) {
    gcc = gcd(gcc, v[i]);
    if (is_pos) {
      if (p[v[i]]) {
        if (has[v[i]] == 0)
          has[v[i]] = 1;
        else
          is_pos = 0;
      } else {
        int h = 0;
        if (v[i] > xx[xx.size() - 1])
          h = xx.size() - 1;
        else {
          h = lower_bound(all(xx), v[i]) - xx.begin();
        }

        for (int j = h; j > -1; j--) {
          if (v[i] % xx[j] == 0) {
            if (has[xx[j]]) {
              is_pos = 0;
              break;
            } else {
              has[xx[j]] = 1;
            }
          }
        }
      }
    }
  }
  if (is_pos) {
    cout << "pairwise coprime" << endl;
  } else if (gcc == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}

////////////////////////////////////////////////////////////////
// Bas aabaju nai javanu///
////////////////////////////////////////////////////////////////

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  //	int t; cin >> t;
  //	while (t--)
  jabru();

  return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////
// Kai baju bhai???


Error: TLE

Fixed submission:

// 😄😄😄😄😄😄😄😄😄😄😄😄😄
// Apno Swagat che//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pi pair<int, int>
#define vp vector<pi>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define For(i, a, b) for (ll i = a; i < b; i++)
#define endl "\\n"
#define debug(x) cout << "AA Baju Smit------> " << #x << " -> " << x << endl
#define all(x) x.begin(), x.end()
#define mint map<int, int>
#define mod 10000007
#define ciN cin
#define X INT_MAX
#define N INT_MIN
////////////////////////////////////////////////////////////////
// Ha Moj Ha///
////////////////////////////////////////////////////////////////
////TEST CASES ATTACHED CHE BHULTO NAI......///////////////////
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
vector<bool> Sieve(int n) {

  vector<bool> is_prime(n + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i] && 1LL * i * i <= n) {
      for (int j = i * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return is_prime;
}

void jabru() {
  vector<bool> p = Sieve(1000000);
  vi xx;
  for (int i = 1; i <= 1000000; i++) {
    if (p[i])
      xx.pb(i);
  }
  int n;
  ciN >> n;
  vi v(n);
  For(i, 0, n) cin >> v[i];
  int gcc = v[0];
  mint has;
  bool is_pos = 1;
  For(i, 0, n) {
    gcc = gcd(gcc, v[i]);
    if (is_pos) {
      int h;
      if (v[i] > xx[xx.size() - 1])
        h = xx.size() - 1;
      else {
        h = lower_bound(all(xx), v[i]) - xx.begin();
      }
      for (int j = h; j > -1; j--) {
        if (v[i] % xx[j] == 0) {
          if (has[xx[j]]) {
            is_pos = 0;
            break;
          } else {
            has[xx[j]] = 1;
          }
        }
      }
    }
  }
  if (is_pos) {
    cout << "pairwise coprime" << endl;
  } else if (gcc == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}

////////////////////////////////////////////////////////////////
// Bas aabaju nai javanu///
////////////////////////////////////////////////////////////////

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  //	int t; cin >> t;
  //	while (t--)
  jabru();

  return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////
// Kai baju bhai???