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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MAX_SIZE 1000001
#define debug(x) cout << #x << " " << x << "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);                                          \\
  freopen("error.txt", "w", stderr);
#define vi(vec, n) vector<ll> vec(n)
#define vio(vec, n, a) vector<ll> vec(n, a)
#define FOR(i, a, b, c)                                                        \\
  for (ll i = a; (c) ? i < (ll)b : i >= (ll)b; (c) ? i += 1 : i -= 1)
#define eb emplace_back
#define mod (ll)(1e9 + 7)
#define all(a, b) memset(a, b, sizeof a)
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)

vio(isprime, MAX_SIZE, true);
vector<ll> prime;
int SPF[MAX_SIZE];
vector<int> cnt(MAX_SIZE);

void manipulated_seive(int N) {
  isprime[0] = isprime[1] = false;
  FOR(i, 2, N, 1) {
    if (isprime[i]) {
      prime.push_back(i);
      SPF[i] = i;
    }
    for (ll j = 0; j < prime.size() && i * prime[j] < N && prime[j] <= SPF[i];
         j++) {
      isprime[i * prime[j]] = false;
      SPF[i * prime[j]] = prime[j];
    }
  }
}

void fact(int x) {
  unordered_map<int, bool> mp;
  while (x != 1) {
    if (!mp[SPF[x]]) {
      cnt[(SPF[x])]++;
      mp[SPF[x]] = 1;
    }
    x = x / SPF[x];
  }
}

bool pairWise() {
  for (int i = 0; i < MAX_SIZE; i++) {
    if (cnt[i] >= 2)
      return false;
  }
  return true;
}

bool setWise(int *ar, int n) {
  int gcd = ar[0];
  for (int i = 1; i < n; i++)
    gcd = __gcd(gcd, ar[i]);
  if (gcd == 1)
    return true;
  return false;
}

int main() {
  // clock_t tStart = clock();
  //  file;
  manipulated_seive(MAX_SIZE);
  fast;

  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  for (int i = 0; i < n; i++) {
    fact(ar[i]);
  }
  if (pairWise()) {
    cout << "pairwise coprime\\n";
    return 0;
  }
  if (setWise(ar, n)) {
    cout << "setwise coprime\\n";
    return 0;
  }
  cout << "not coprime\\n";

  // printf("Time taken: %.6fs\\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MAX_SIZE 1000001
#define debug(x) cout << #x << " " << x << "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);                                          \\
  freopen("error.txt", "w", stderr);
#define vi(vec, n) vector<ll> vec(n)
#define vio(vec, n, a) vector<ll> vec(n, a)
#define FOR(i, a, b, c)                                                        \\
  for (ll i = a; (c) ? i < (ll)b : i >= (ll)b; (c) ? i += 1 : i -= 1)
#define eb emplace_back
#define mod (ll)(1e9 + 7)
#define all(a, b) memset(a, b, sizeof a)
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)

vio(isprime, MAX_SIZE, true);
vector<ll> prime;
int SPF[MAX_SIZE];
vector<int> cnt(MAX_SIZE);

void manipulated_seive(int N) {
  isprime[0] = isprime[1] = false;
  FOR(i, 2, N, 1) {
    if (isprime[i]) {
      prime.push_back(i);
      SPF[i] = i;
    }
    for (ll j = 0; j < prime.size() && i * prime[j] < N && prime[j] <= SPF[i];
         j++) {
      isprime[i * prime[j]] = false;
      SPF[i * prime[j]] = prime[j];
    }
  }
}

void fact(int x) {
  bool done[MAX_SIZE];
  memset(done, 0, sizeof done);
  while (x != 1) {
    if (!done[SPF[x]]) {
      cnt[(SPF[x])]++;
      done[SPF[x]] = 1;
    }
    x = x / SPF[x];
  }
}

void primeFactor(int *ar, int n) {
  for (int i = 0; i < n; i++) {
    fact(ar[i]);
  }
}

bool pairWise(int *ar, int n) {
  for (int i = 0; i < MAX_SIZE; i++) {
    if (cnt[i] >= 2)
      return false;
  }
  return true;
}

bool setWise(int *ar, int n) {
  int gcd = ar[0];
  for (int i = 1; i < n; i++)
    gcd = __gcd(gcd, ar[i]);
  if (gcd == 1)
    return true;
  return false;
}

int main() {
  // clock_t tStart = clock();
  //  file;
  manipulated_seive(MAX_SIZE);
  fast;

  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  primeFactor(ar, n);
  if (pairWise(ar, n)) {
    cout << "pairwise coprime\\n";
    return 0;
  }
  if (setWise(ar, n)) {
    cout << "setwise coprime\\n";
    return 0;
  }
  cout << "not coprime\\n";

  // printf("Time taken: %.6fs\\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
  return 0;
}