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

#include <bits/stdc++.h>
#define int ll
#define M 1000000007
#define hell 998244353
#define inf 1e18
#define pb push_back
#define pi 3.1415926536
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define all(a) a.begin(), a.end()
#define ll long long
#define vec vector<int>
#define pii pair<int, int>
#define endl "\\n"
#define gcd __gcd
using namespace std;
int power(int x, unsigned int y, unsigned int m) {
  if (y == 0)
    return 1;
  int p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}
int modInverse(int a, int m) { return power(a, m - 2, m); }
int nCrModPFermat(int n, int r, int p) {
  if (r == 0)
    return 1;

  int fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % p;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
bool pr[1000001];
void seive_of_eras(int n) {
  pr[0] = pr[1] = true;
  for (int i = 2; i <= n; i++) {
    if (pr[i] == false) {
      for (int j = 2 * i; j <= n; j += i) {
        pr[j] = true;
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll t = 1;
  // cin >> t;
  /*SAARE INPUTS LENA*/
  /*VARIABLES KI RANGE DEKHLENA*/
  /*EXPONENT RANGE OF OUTPUT KA DHYAN RAKHNA*/
  while (t-- > 0) {
    ll n = 0;
    cin >> n;
    vector<int> v(n);
    vector<vector<int>> pfac(n);
    vector<vector<int>> prime(1000000);
    seive_of_eras(1000000);
    for (int i = 0; i < n; ++i) {
      cin >> v[i];
      int x = v[i];
      while (x != 1) {
        // int d=0;
        if (pr[x] == false) {
          pfac[i].pb(x);
          prime[x].pb(i);
          x = 1;
        }
        for (int j = 2; j <= sqrt(x); j++) {
          if (x % j == 0) {
            // d=1;
            prime[j].pb(i);
            pfac[i].pb(j);
            while (x % j == 0) {
              x /= j;
            }
          }
        }
      }
    }
    int f = 0;
    for (int i = n - 2; i >= 0; --i) {
      for (int j = 0; j < pfac[i].size(); j++) {
        for (int k = 0; k < prime[pfac[i][j]].size(); k++) {
          if (prime[pfac[i][j]][k] > i) {
            f = 1;
            break;
          }
        }
        if (f == 1)
          break;
      }
      if (f == 1)
        break;
    }
    if (f == 0)
      cout << "pairwise coprime";
    else {
      int g = v[0];
      for (int i = 0; i < n; ++i) {
        g = gcd(v[i], g);
        if (g == 1) {
          f = 2;
          break;
        }
      }
      if (f == 2)
        cout << "setwise coprime";
      else
        cout << "not coprime";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int ll
#define M 1000000007
#define hell 998244353
#define inf 1e18
#define pb push_back
#define pi 3.1415926536
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define all(a) a.begin(), a.end()
#define ll long long
#define vec vector<int>
#define pii pair<int, int>
#define endl "\\n"
#define gcd __gcd
using namespace std;
int power(int x, unsigned int y, unsigned int m) {
  if (y == 0)
    return 1;
  int p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}
int modInverse(int a, int m) { return power(a, m - 2, m); }
int nCrModPFermat(int n, int r, int p) {
  if (r == 0)
    return 1;

  int fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % p;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll t = 1;
  // cin >> t;
  /*SAARE INPUTS LENA*/
  /*VARIABLES KI RANGE DEKHLENA*/
  /*EXPONENT RANGE OF OUTPUT KA DHYAN RAKHNA*/
  while (t-- > 0) {
    ll n = 0;
    cin >> n;
    vector<int> v(n);
    vector<vector<int>> pfac(n);
    vector<vector<int>> prime(1000000);
    for (int i = 0; i < n; ++i) {
      cin >> v[i];
      int x = v[i];
      while (x != 1) {
        int d = 0;
        for (int j = 2; j <= sqrt(x); j++) {
          if (x % j == 0) {
            d = 1;
            prime[j].pb(i);
            pfac[i].pb(j);
            while (x % j == 0) {
              x /= j;
            }
          }
        }
        if (d == 0) {
          pfac[i].pb(x);
          prime[x].pb(i);
          x = 1;
        }
      }
    }
    int f = 0;
    for (int i = n - 2; i >= 0; --i) {
      for (int j = 0; j < pfac[i].size(); j++) {
        for (int k = 0; k < prime[pfac[i][j]].size(); k++) {
          if (prime[pfac[i][j]][k] > i) {
            f = 1;
            break;
          }
        }
        if (f == 1)
          break;
      }
      if (f == 1)
        break;
    }
    if (f == 0)
      cout << "pairwise coprime";
    else {
      int g = v[0];
      for (int i = 0; i < n; ++i) {
        g = gcd(v[i], g);
        if (g == 1) {
          f = 2;
          break;
        }
      }
      if (f == 2)
        cout << "setwise coprime";
      else
        cout << "not coprime";
    }
  }
  return 0;
}