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

#include <bits/stdc++.h>

using namespace std;

vector<int> primes;

namespace Sieve {
vector<bool> ispr;
vector<int> prs;
void work(int n) {
  int k = sqrt(n) + 2;
  while (k * k > n)
    k--;
  vector<bool> f(k + 1, 1);
  f[0] = f[1] = 0;
  ispr.resize(n + 1);
  ispr.shrink_to_fit();
  prs.clear();
  for (int i = 2; i <= k; i++)
    if (f[i]) {
      for (int j = i * i; j <= k; j += i) {
        f[j] = 0;
      }
      ispr[i] = 1;
      prs.push_back(i);
    }
  vector<int> g(k + 1);
  int c = (int)prs.size();
  for (int i = k + 1; i <= n; i += k) {
    int j = min(i + k - 1, n);
    for (int u = 0; u < c; u++) {
      int p = prs[u];
      for (int q = (i + p - 1) / p * p; q <= j; q += p) {
        g[q - i] = i;
      }
    }
    for (int u = i; u <= j; u++)
      if (g[u - i] < i) {
        ispr[u] = 1;
        prs.push_back(u);
      }
  }
}
} // namespace Sieve

int main() {
  Sieve::work((int)1e6 + 7);
  int n;
  cin >> n;
  vector<int> a(n);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  };

  int G = a[0];
  for (int i = 1; i < n; i++) {
    G = __gcd(G, a[i]);
  };

  bool ok = true;

  int cnt[(int)1e6 + 7];
  memset(cnt, 0, sizeof cnt);

  for (int i = 0; i < n && ok; i++) {
    int nums = a[i];
    for (int d : Sieve::prs) {
      if (d * d > nums)
        break;
      if (nums % d == 0) {
        cnt[d]++;
        if (cnt[d] > 1) {
          ok = false;
          break;
        };
      }
      while (nums % d == 0) {
        nums /= d;
      }
    }
    if (nums > 1) {
      cnt[nums]++;
      if (cnt[nums] > 1) {
        ok = false;
        break;
      };
    }
  };

  if (ok) {
    cout << "pairwise coprime"
         << "\\n";
    return 0;
  };

  if (G == 1) {
    cout << "setwise coprime"
         << "\\n";
    return 0;
  };

  cout << "not coprime"
       << "\\n";

  return 0;
};


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<int> primes;

void sieve(int N) {
  bool isPrime[N + 1];
  for (int i = 0; i <= N; ++i) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i * i <= N; ++i) {
    if (isPrime[i] == true) {
      for (int j = i * i; j <= N; j += i)
        isPrime[j] = false;
    }
  }
  for (int i = 2; i <= N; i++) {
    if (isPrime[i] == true) {
      primes.push_back(i);
    };
  };
}

int main() {
  sieve((int)1e6 + 7);
  int n;
  cin >> n;
  vector<int> a(n);
  set<int> s;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s.insert(a[i]);
  };

  int G = a[0];
  for (int i = 1; i < n; i++) {
    G = __gcd(G, a[i]);
  };

  bool ok = true;

  int cnt[(int)1e6 + 7];
  memset(cnt, 0, sizeof cnt);

  for (int i = 0; i < n; i++) {
    int nums = a[i];
    for (int xx = 0; nums > 1 && xx <= (int)1e6; xx++) {
      int p = primes[xx];
      if (nums % p == 0) {
        cnt[p]++;
      };
      while (nums % p == 0)
        nums /= p;
    };
  };

  for (int i = 2; i <= (int)1e6; i++) {
    if (cnt[i] > 1) {
      ok = false;
      break;
    };
  };

  if (ok) {
    cout << "pairwise coprime"
         << "\\n";
    return 0;
  };

  if (G == 1) {
    cout << "setwise coprime"
         << "\\n";
    return 0;
  };

  cout << "not coprime"
       << "\\n";

  return 0;
};