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

#include <bits/stdc++.h>
#define lld long long int
#define vec vector<lld>
#define pll pair<lld, lld>
#define pb push_back
#define ss second
#define ff first
#define mp make_pair
#define mod 1000000007
#define answer(x) cout << endl << "answer : " << x << endl << endl;
#define op(x) cout << x << endl;
#define maxn 500005
#define messi                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int main() {
  messi lld t = 1; // cin>>t;
  while (t--) {
    lld n = 0, m = 0, k = 0, i = 0, j = 0, p = 0, q = 0, x = 0, y = 0, z = 0,
        ans = 0, cnt = 0, l = 0, r = 0, mid = 0, lo = 0, hi = 0;
    string s;
    bool flag = false;
    cin >> n;
    lld a[n];
    for (i = 0; i < n; i++)
      cin >> a[i];
    lld gcd = 0;
    for (i = 0; i < n; i++)
      gcd = __gcd(gcd, a[i]);
    if (gcd == 1)
      j = 0;
    else
      j = 1;
    if (j == 1)
      cout << "not coprime" << endl;
    else {
      lld vv[1000001];
      memset(vv, 0, sizeof(vv));
      for (i = 0; i < n; i++) {
        x = a[i];
        for (j = 2; j * j <= x; j++) {
          if (x % j == 0)
            vv[j]++;
          while (x % j == 0)
            x /= j;
          if (vv[j] >= 2) {
            flag = true;
            break;
          }
        }
        if (x > 1)
          vv[x]++;
        if (vv[x] >= 2) {
          flag = true;
          break;
        }
        if (flag)
          break;
      }
      if (flag)
        cout << "setwise coprime" << endl;
      else
        cout << "pairwise coprime" << endl;
    }
  }
  return 0;
}
/*  CODED BY:-
 ________________________________________
|            __        __                |
|  /\\   /\\  |__)  /\\  |  \\ |__| \\ /  /\\  |
| /~~\\ /~~\\ |  \\ /~~\\ |__/ |  |  |  /~~\\ |
|________________________________________|

*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lld long long int
#define vec vector<lld>
#define pll pair<lld, lld>
#define pb push_back
#define ss second
#define ff first
#define mp make_pair
#define mod 1000000007
#define answer(x) cout << endl << "answer : " << x << endl << endl;
#define op(x) cout << x << endl;
#define maxn 500005
#define messi                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
vector<char> is_prime(1000000 + 1, true);
void seive() {
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i * i <= 1000000; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j <= 1000000; j += i)
        is_prime[j] = false;
    }
  }
  return;
}
int main() {
  messi lld t = 1; // cin>>t;
  while (t--) {
    lld n = 0, m = 0, k = 0, i = 0, j = 0, p = 0, q = 0, x = 0, y = 0, z = 0,
        ans = 0, cnt = 0, l = 0, r = 0, mid = 0, lo = 0, hi = 0;
    string s;
    bool flag = false;
    cin >> n;
    lld a[n];
    for (i = 0; i < n; i++)
      cin >> a[i];
    lld gcd = 0;
    for (i = 0; i < n; i++)
      gcd = __gcd(gcd, a[i]);
    if (gcd == 1)
      j = 0;
    else
      j = 1;
    if (j == 1)
      cout << "not coprime" << endl;
    else {
      vec v;
      seive();
      for (i = 2; i <= 1000000; i++)
        if (is_prime[i] == true)
          v.pb(i);
      m = v.size();
      lld viz[m];
      memset(viz, 0, sizeof(viz));
      for (i = 0; i < n; i++) {
        x = a[i];
        for (j = 0; j < m; j++) {
          if (x % v[j] == 0)
            viz[j]++;
          if (viz[j] >= 2) {
            flag = true;
            break;
          }
        }
        if (flag)
          break;
      }
      if (flag)
        cout << "setwise coprime" << endl;
      else
        cout << "pairwise coprime" << endl;
    }
  }
  return 0;
}
/*  CODED BY:-
 ________________________________________
|            __        __                |
|  /\\   /\\  |__)  /\\  |  \\ |__| \\ /  /\\  |
| /~~\\ /~~\\ |  \\ /~~\\ |__/ |  |  |  /~~\\ |
|________________________________________|

*/