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

/******Old_school_@23******/
/******Why_would_a_compiler_lie?******/
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
#define lld long long int
#define ld long double
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define F first
#define S second
#define nl '\\n'
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(c) (c).size()
#define tr(x, a) for (auto &a : x)
#define psnt(x, a) (x).find(a) != (x).end()
#define vpsnt(x, a) find(all(x), a) != (x).end()
#define lexichar(x, y)                                                         \\
  lexicographical_compare(x, x + strlen(x), y,                                 \\
                          y + strlen(y)) // modify the ranges
#define lexistr(x, y)                                                          \\
  lexicographical_compare(x.begin(), x.end(), y.begin(), y.end())
#define MOD 1000000007
#define pi 3.1415926536
#define tod 1000005
#define itr(i, a, b) for (lld i = a; i <= b; i++)
#define itrn(i, a, b) for (lld i = a; i >= b; i--)
#define iot(n) for (lld i = 0; i < n; i++)
#define pls(n, arr)                                                            \\
  lld arr[n];                                                                  \\
  iot(n) cin >> arr[i];
#define bye fflush(stdout)

typedef pair<lld, lld> pii;
typedef pair<string, lld> psi;
template <typename T> bool mycomp(T x, T y) {
  return (x == y); // give your condition here
}
bool paircomp(const pair<lld, lld> &x, const pair<lld, lld> &y) {
  return x.second < y.second;
}
vector<lld> sv;
void sieve(lld N) {
  bool isPrime[N + 1];
  itr(i, 3, N) {
    if (i % 2 != 0)
      isPrime[i] = true;
    else
      isPrime[i] = false;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  isPrime[2] = true;
  for (lld i = 2; i <= N; i++) {
    if (isPrime[i] == true) { // Mark all the multiples of i as composite
                              // numbers
      for (lld j = i * i; j <= N; j += i)
        isPrime[j] = false;
    }
  }

  iot(N) if (isPrime[i]) sv.pb(i);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  sieve(tod);
  lld n;
  cin >> n;
  pls(n, a);
  lld hash[tod] = {0};
  lld fg = 1;
  iot(n) {
    hash[a[i]]++;
    if (hash[a[i]] > 1 && a[i] != 1) {
      fg = -1;
      break;
    }
  }
  if (fg == -1) {
    lld g = a[0];
    iot(n) g = __gcd(g, a[i]);
    if (g == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
    return 0;
  }
  for (auto &i : sv) {
    lld cc = 0;
    lld p = i;
    while (p < tod) {
      if (hash[p])
        cc++;

      p += i;
      if (cc > 1)
        break;
    }
    if (cc > 1) {
      fg = -1;
      break;
    }
  }
  if (fg == 1) {
    cout << "pairwise coprime";
    return 0;
  }
  lld g = a[0];
  iot(n) g = __gcd(g, a[i]);
  if (g == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}


Error: TLE

Fixed submission:

/******Old_school_@23******/
/******Why_would_a_compiler_lie?******/
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
#define lld long long int
#define ld long double
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define F first
#define S second
#define nl '\\n'
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(c) (c).size()
#define tr(x, a) for (auto &a : x)
#define psnt(x, a) (x).find(a) != (x).end()
#define vpsnt(x, a) find(all(x), a) != (x).end()
#define lexichar(x, y)                                                         \\
  lexicographical_compare(x, x + strlen(x), y,                                 \\
                          y + strlen(y)) // modify the ranges
#define lexistr(x, y)                                                          \\
  lexicographical_compare(x.begin(), x.end(), y.begin(), y.end())
#define MOD 1000000007
#define pi 3.1415926536
#define tod 1
#define itr(i, a, b) for (lld i = a; i <= b; i++)
#define itrn(i, a, b) for (lld i = a; i >= b; i--)
#define iot(n) for (lld i = 0; i < n; i++)
#define pls(n, arr)                                                            \\
  lld arr[n];                                                                  \\
  iot(n) cin >> arr[i];
#define bye fflush(stdout)

typedef pair<lld, lld> pii;
typedef pair<string, lld> psi;
template <typename T> bool mycomp(T x, T y) {
  return (x == y); // give your condition here
}
bool paircomp(const pair<lld, lld> &x, const pair<lld, lld> &y) {
  return x.second < y.second;
}
lld hashr[1000005];
#define MAXN 1000005
lld spf[MAXN];

void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    spf[i] = i;

  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
void getFactorization(lld x) {
  lld flag[MAXN] = {0};
  vector<lld> ret;
  while (x != 1) {
    flag[spf[x]]++;
    if (flag[spf[x]] == 1)
      hashr[spf[x]]++;
    if (hashr[spf[x]] > 1)
      return;
    x = x / spf[x];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  sieve();
  lld n;
  cin >> n;
  pls(n, a);
  iot(n) getFactorization(a[i]);
  lld fg = 1;
  iot(MAXN) {
    if (hashr[i] > 1) {
      fg = -1;
      break;
    }
  }
  if (fg == 1) {
    cout << "pairwise coprime";
    return 0;
  }
  lld g = a[0];
  iot(n) { g = __gcd(g, a[i]); }
  if (g == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}