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

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

// --------------------------------------------------------------------------------
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mset(arr, val) memset(arr, val, sizeof(arr))
#define frepi(a, b) for (int i = a; i <= b; ++i)
#define int long long
// --------------------------------------------------------------------------------

void test_case(int tc) {
  int n;
  cin >> n;
  int primes[1000009];
  mset(primes, 0);
  primes[0] = 1, primes[1] = 1;
  for (int i = 2; i * i < 1000009; ++i) {
    if (primes[i] == 0) {
      for (int j = i * i; j < 1000009; j += i) {
        primes[j] = 1;
      }
    }
  }
  int arr[n];
  frepi(0, n - 1) cin >> arr[i];
  int gcd = arr[0];
  for (int i = 1; i < n; ++i)
    gcd = (__gcd(arr[i], gcd));
  if (gcd != 1) {
    cout << "not coprime";
    return;
  }
  set<int> factors;
  for (int j = 0; j < n; ++j) {
    int x = arr[j];
    if (arr[j] == 1)
      continue;
    if (x % 2 == 0) {
      if (factors.find(2) != factors.end()) {
        cout << "setwise coprime";
        return;
      }
      factors.insert(2);
      while (x % 2 == 0)
        x /= 2;
    }
    if (x % 3 == 0) {
      if (factors.find(3) != factors.end()) {
        cout << "setwise coprime";
        return;
      }
      factors.insert(3);
      while (x % 3 == 0)
        x /= 3;
    }
    for (int i = 5; i * i <= x; i += 6) {
      if ((x % i) == 0) {
        if (primes[i] == 0) {
          if (factors.find(i) != factors.end()) {
            cout << "setwise coprime";
            return;
          }
          factors.insert(i);
          while (x % i == 0)
            x /= i;
        }
      }
      if ((x % (i + 2)) == 0) {
        if (primes[i + 2] == 0) {
          if (factors.find(i + 2) != factors.end()) {
            cout << "setwise coprime";
            return;
          }
          factors.insert(i + 2);
          while (x % (i + 2) == 0)
            x /= (i + 2);
        }
      }
    }
    if (x > 1) {
      if (factors.find(x) != factors.end()) {
        cout << "setwise coprime";
        return;
      }
      factors.insert(x);
    }
  }
  cout << "pairwise coprime";
}

int32_t main() {
  FASTIO;
#ifndef ONLINE_JUDGE
  freopen("/home/suryakant/input.txt", "r", stdin);
  freopen("/home/suryakant/output.txt", "w", stdout);
#endif

  int tc = 1;
  // cin>>tc;
  for (int i = 1; i <= tc; ++i)
    test_case(tc);
}

Error: TLE

Fixed submission:

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

// --------------------------------------------------------------------------------
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mset(arr, val) memset(arr, val, sizeof(arr))
#define frepi(a, b) for (int i = a; i <= b; ++i)
#define int long long
// --------------------------------------------------------------------------------

void test_case(int tc) {
  int n;
  cin >> n;
  int primes[1000009];
  mset(primes, 0);
  primes[0] = 1, primes[1] = 1;
  for (int i = 2; i * i < 1000009; ++i) {
    if (primes[i] == 0) {
      for (int j = i; j < 1000009; j += i) {
        if (primes[j] == 0)
          primes[j] = i;
      }
    }
  }
  int arr[n];
  frepi(0, n - 1) cin >> arr[i];
  int gcd = arr[0];
  for (int i = 1; i < n; ++i)
    gcd = (__gcd(arr[i], gcd));
  if (gcd != 1) {
    cout << "not coprime";
    return;
  }
  set<int> factors;
  for (int i = 0; i < n; ++i) {
    int x = arr[i];
    if (arr[i] == 1)
      continue;
    while (x > 1) {
      int div = primes[x];
      if (div == 0)
        continue;
      if (factors.find(div) != factors.end()) {
        cout << "setwise coprime";
        return;
      }
      factors.insert(div);
      while (x % div == 0 && x > 1)
        x /= div;
    }
  }
  cout << "pairwise coprime";
}

int32_t main() {
  FASTIO;
#ifndef ONLINE_JUDGE
  freopen("/home/suryakant/input.txt", "r", stdin);
  freopen("/home/suryakant/output.txt", "w", stdout);
#endif

  int tc = 1;
  // cin>>tc;
  for (int i = 1; i <= tc; ++i)
    test_case(tc);
}