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

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back

#define int int64_t
#define ld long double

const int MOD = 1e9 + 7;
const int N = 1e6 + 5;

vector<int> primes;
bool isPrime[N];
int cnt[N];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  int arr[n];
  int gcd = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    gcd = __gcd(gcd, arr[i]);
  }

  for (int i = 2; i < N; i++) {
    if (!isPrime[i]) {
      primes.pb(i);
      for (int j = i * i; j < N; j += i)
        isPrime[j] = 1;
    }
  }
  bool f = 1;
  for (int i = 0; i < n; i++) {
    for (int k : primes) {
      if (k * k > arr[i])
        break;
      if (isPrime[k])
        break;
      if (arr[i] % k == 0) {
        cnt[k]++;
        if (cnt[k] > 1) {
          if (gcd == 1) {
            cout << "setwise coprime";
          } else {
            cout << "not coprime";
          }
          return 0;
        }
        while (arr[i] % k == 0)
          arr[i] /= k;
      }
    }
    if (arr[i] > 1) {
      cnt[arr[i]]++;
      if (cnt[arr[i]] > 1) {
        if (gcd == 1) {
          cout << "setwise coprime";
        } else {
          cout << "not coprime";
        }
        return 0;
      }
    }
  }
  for (int i = 2; i < N; i++) {
    if (cnt[i] > 1) {
      f = 0;
      break;
    }
  }
  if (gcd != 1) {
    cout << "not coprime";
    return 0;
  }
  if (f) {
    cout << "pairwise coprime";
  } else if (gcd == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back

#define int int64_t
#define ld long double

const int MOD = 1e9 + 7;
const int N = 1e6 + 5;

vector<int> primes;
bool isPrime[N];
int cnt[N];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  int arr[n];
  int gcd = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    gcd = __gcd(gcd, arr[i]);
  }

  for (int i = 2; i < N; i++) {
    if (!isPrime[i]) {
      primes.pb(i);
      for (int j = i * i; j < N; j += i)
        isPrime[j] = 1;
    }
  }
  bool f = 1;
  for (int i = 0; i < n; i++) {
    for (int k : primes) {
      if (k > arr[i] * arr[i])
        break;
      if (isPrime[k])
        break;
      if (arr[i] % k == 0) {
        cnt[k]++;
        if (cnt[k] > 1) {
          if (gcd == 1) {
            cout << "setwise coprime";
          } else {
            cout << "not coprime";
          }
          return 0;
        }
        while (arr[i] % k == 0)
          arr[i] /= k;
      }
    }
    if (arr[i] > 1) {
      cnt[arr[i]]++;
      if (cnt[arr[i]] > 1) {
        if (gcd == 1) {
          cout << "setwise coprime";
        } else {
          cout << "not coprime";
        }
        return 0;
      }
    }
  }
  for (int i = 2; i < N; i++) {
    if (cnt[i] > 1) {
      f = 0;
      break;
    }
  }
  if (gcd != 1) {
    cout << "not coprime";
    return 0;
  }
  if (f) {
    cout << "pairwise coprime";
  } else if (gcd == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }

  return 0;
}