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

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define fi first
#define se second
#define inf (INT_MAX / 2 - 1)
#define infl (1LL << 60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a), end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr << (#x) << ": " << (x) << '\\n', aaa
#define dbga(x, n)                                                             \\
  cerr << (#x) << "[]: ";                                                      \\
  for (int _ = 0; _ < n; _++)                                                  \\
    cerr << x[_] << ' ';                                                       \\
  cerr << '\\n', aaa
#define dbgs(x)                                                                \\
  cerr << (#x) << "[stl]: ";                                                   \\
  for (auto _ : x)                                                             \\
    cerr << _ << ' ';                                                          \\
  cerr << '\\n', aaa
#define dbgp(x) cerr << (#x) << ": " << x.fi << ' ' << x.se << '\\n', aaa
#define maxn 1000000
#define maxp 78498

using namespace std;

bool cc[maxn + 5], viz[maxp + 5];
int v[maxn + 5];
vi prime;

void ciur() {
  int i, j;
  cc[0] = cc[1] = 1;
  for (i = 2; i * i <= maxn; i++)
    if (!cc[i])
      for (j = i * i; j <= maxn; j += i)
        cc[j] = 1;
  for (i = 2; i <= maxn; i++)
    if (!cc[i])
      prime.pb(i);
}

int gcd(int a, int b) {
  if (b > 0)
    return gcd(b, a % b);
  return a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ciur();
  int n;
  cin >> n;
  int i, j, z;
  for (i = 1; i <= n; i++)
    cin >> v[i];
  bool cop = 1, dv;
  for (i = 1; i <= n && cop; i++) {
    z = v[i];
    j = 0;
    while (z > 1 && j < sz(prime) && 1LL * prime[j] * prime[j] <= 1LL * v[i] &&
           cop) {
      dv = 0;
      while (z > 1 && z % prime[j] == 0) {
        dv = 1;
        z /= prime[j];
      }
      if (dv) {
        cop &= (!viz[j]);
        viz[j] = 1;
      }
      j++;
    }
    if (z > 1) {
      j = lower_bound(all(prime), z) - prime.begin();
      cop &= (!viz[j]);
      viz[j] = 1;
    }
  }
  if (cop)
    cout << "pairwise coprime";
  else {
    int ans = 0;
    for (i = 1; i <= n; i++)
      ans = gcd(v[i], ans);
    if (ans == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define fi first
#define se second
#define inf (INT_MAX / 2 - 1)
#define infl (1LL << 60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a), end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr << (#x) << ": " << (x) << '\\n', aaa
#define dbga(x, n)                                                             \\
  cerr << (#x) << "[]: ";                                                      \\
  for (int _ = 0; _ < n; _++)                                                  \\
    cerr << x[_] << ' ';                                                       \\
  cerr << '\\n', aaa
#define dbgs(x)                                                                \\
  cerr << (#x) << "[stl]: ";                                                   \\
  for (auto _ : x)                                                             \\
    cerr << _ << ' ';                                                          \\
  cerr << '\\n', aaa
#define dbgp(x) cerr << (#x) << ": " << x.fi << ' ' << x.se << '\\n', aaa
#define maxn 1000000
#define maxp 78498

using namespace std;

bool cc[maxn + 5], viz[maxp + 5];
int v[maxn + 5];
vi prime;

void ciur() {
  int i, j;
  cc[0] = cc[1] = 1;
  for (i = 2; i * i <= maxn; i++)
    if (!cc[i])
      for (j = i * i; j <= maxn; j += i)
        cc[j] = 1;
  for (i = 2; i <= maxn; i++)
    if (!cc[i])
      prime.pb(i);
}

int gcd(int a, int b) {
  if (b > 0)
    return gcd(b, a % b);
  return a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ciur();
  int n;
  cin >> n;
  int i, j, z;
  for (i = 1; i <= n; i++)
    cin >> v[i];
  bool cop = 1, dv;
  for (i = 1; i <= n && cop; i++) {
    z = v[i];
    j = 0;
    while (z > 1 && j < sz(prime) && cop) {
      dv = 0;
      while (z > 1 && z % prime[j] == 0) {
        dv = 1;
        z /= prime[j];
      }
      if (dv) {
        cop &= (!viz[j]);
        viz[j] = 1;
      }
      j++;
    }
  }
  if (cop)
    cout << "pairwise coprime";
  else {
    int ans = 0;
    for (i = 1; i <= n; i++)
      ans = gcd(v[i], ans);
    if (ans == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
  return 0;
}