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

#include <bits/stdc++.h>
// For policy based data structure
/*#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> */
using namespace std;
/*using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;*/

#define MD 1000000007
#define X first
#define Y second
#define pb push_back
#define debug(val, ch) cout << val << ch

typedef long long int ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<int, bool> pib;
typedef pair<bool, int> pbi;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
void sieve(vector<bool> &isprime) {
  int i, j, k;
  for (i = 2; i <= sqrt(1000000); i++) {
    if (!isprime[i]) {
      for (j = 2 * i; j <= 1000000; j += i) {
        isprime[j] = 1;
      }
    }
  }
}
/*********************************************************************/
void test(int r) {
  vector<bool> pr(1e6 + 1, 0);
  sieve(pr);
  vector<int> pi;
  int n;
  cin >> n;
  vector<int> v(n);
  int i;
  for (i = 0; i < n; i++)
    cin >> v[i];
  for (i = 2; i <= 1e6; i++) {
    if (!pr[i]) {
      pi.pb(i);
    }
  }
  // cout<<pi.size()<<endl;
  vector<int> pc(1e6 + 1, 0);
  for (i = 0; i < n; i++) {
    int j;
    int x = v[i];
    for (j = 0; pi[j] <= sqrt(x) && j < pi.size(); j++) {
      if (x % pi[j] == 0) {
        pc[pi[j]]++;
        while (x - (x / pi[j]) * pi[j] == 0) {
          x = x / pi[j];
        }
      }
      if (x == 1)
        break;
    }
    if (x != 1)
      pc[x]++;
  }
  int f = 0;
  for (i = 2; i <= 1e6; i++) {
    f = max(f, pc[i]);
  }
  if (f <= 1) {
    cout << "pairwise coprime";
  } else if (f != n) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}
/***********************************************************************/
int main() {
  fast();
  int t = 1, r;
  // cin>>t;
  for (r = 1; r <= t; r++)
    test(r);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// For policy based data structure
/*#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> */
using namespace std;
/*using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;*/

#define MD 1000000007
#define X first
#define Y second
#define pb push_back
#define debug(val, ch) cout << val << ch

typedef long long int ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<int, bool> pib;
typedef pair<bool, int> pbi;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
void sieve(vector<bool> &isprime) {
  int i, j, k;
  for (i = 2; i <= sqrt(1000000); i++) {
    if (!isprime[i]) {
      for (j = 2 * i; j <= 1000000; j += i) {
        isprime[j] = 1;
      }
    }
  }
}
/*********************************************************************/
void test(int r) {
  vector<bool> pr(1e6 + 1, 0);
  sieve(pr);
  vector<int> pi;
  int n;
  cin >> n;
  vector<int> v(n);
  int i;
  for (i = 0; i < n; i++)
    cin >> v[i];
  for (i = 2; i <= 1e6; i++) {
    if (!pr[i]) {
      pi.pb(i);
    }
  }
  vector<int> pc(1e6 + 1, 0);
  for (i = 0; i < n; i++) {
    int j;
    int x = v[i];
    for (j = 0; pi[j] <= x && j < pi.size(); j++) {
      if (x % pi[j] == 0) {
        pc[pi[j]]++;
        while (x - (x / pi[j]) * pi[j] == 0) {
          x = x / pi[j];
        }
      }
      if (x == 1)
        break;
    }
    if (x != 1)
      pc[x]++;
  }
  int f = 0;
  for (i = 2; i <= 1e6; i++) {
    f = max(f, pc[i]);
  }
  if (f <= 1) {
    cout << "pairwise coprime";
  } else if (f != n) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}
/***********************************************************************/
int main() {
  fast();
  int t = 1, r;
  // cin>>t;
  for (r = 1; r <= t; r++)
    test(r);
  return 0;
}