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

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

#ifdef LOCAL
#include "debug.h"
#define input                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#else
#define debug(...) 4
#define input 4
#endif

using ll = long long;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;

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

  for (int i = 3; 1LL * i * i < N; i++) {
    if (spf[i] == i) {
      for (int j = 1LL * i * i; j < N; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
// optimized by logn of map;
vector<pair<int, int>> get(int x) {
  vector<pair<int, int>> ret;
  while (x != 1) {
    if (ret.size() == 0) {
      ret.push_back(make_pair(spf[x], 1));

    } else {
      if (ret.back().first == spf[x]) {
        ret.back().second++;
      } else {
        ret.push_back(make_pair(spf[x], 1));
      }
    }

    x = x / spf[x];
  }

  return ret;
}

using T = int;
vector<T> BuildDivisorsFromFactors(const vector<pair<T, int>> &factors) {
  vector<T> divisors = {1};
  for (auto &p : factors) {
    int sz = (int)divisors.size();
    for (int i = 0; i < sz; i++) {
      T cur = divisors[i];
      for (int j = 0; j < p.second; j++) {
        cur *= p.first;
        divisors.push_back(cur);
      }
    }
  }
  sort(divisors.begin(), divisors.end());
  return divisors;
}
int f[N];
signed main() {
  input;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  sieve();
  int n;
  cin >> n;
  vector<int> a(n);
  int flag = 0;
  int g = 0;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    g = __gcd(g, a[i]);
  }
  if (g != 1) {
    cout << "not coprime";
    return 0;
  }
  map<int, vector<int>> cache;
  for (int i = 0; i < a.size(); i++) {
    if (cache.count(a[i])) {
      auto d = cache[a[i]];
      for (int i = 1; i < d.size(); i++) {
        if (f[d[i]] > 0) {
          flag = 1;
        }
        f[d[i]]++;
      }
    } else {
      auto fac = get(a[i]);
      auto d = BuildDivisorsFromFactors(fac);
      cache[a[i]] = d;
      for (int i = 1; i < d.size(); i++) {
        if (f[d[i]] > 0) {
          flag = 1;
        }
        f[d[i]]++;
      }
    }
  }
  if (flag == 0) {
    cout << "pairwise coprime";
  } else if (g == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}


Error: TLE

Fixed submission:

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

#ifdef LOCAL
#include "debug.h"
#define input                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#else
#define debug(...) 4
#define input 4
#endif

using ll = long long;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;

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

  for (int i = 3; 1LL * i * i < N; i++) {
    if (spf[i] == i) {
      for (int j = 1LL * i * i; j < N; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
// optimized by logn of map;
vector<pair<int, int>> get(int x) {
  vector<pair<int, int>> ret;
  while (x != 1) {
    if (ret.size() == 0) {
      ret.push_back(make_pair(spf[x], 1));

    } else {
      if (ret.back().first == spf[x]) {
        ret.back().second++;
      } else {
        ret.push_back(make_pair(spf[x], 1));
      }
    }

    x = x / spf[x];
  }

  return ret;
}

using T = int;
vector<T> BuildDivisorsFromFactors(const vector<pair<T, int>> &factors) {
  vector<T> divisors = {1};
  for (auto &p : factors) {
    int sz = (int)divisors.size();
    for (int i = 0; i < sz; i++) {
      T cur = divisors[i];
      for (int j = 0; j < p.second; j++) {
        cur *= p.first;
        divisors.push_back(cur);
      }
    }
  }
  sort(divisors.begin(), divisors.end());
  return divisors;
}
int f[N];
signed main() {
  input;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  sieve();
  int n;
  cin >> n;
  int a[n];
  int flag = 0;
  int g = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    g = __gcd(g, a[i]);
    auto fac = get(a[i]);
    auto d = BuildDivisorsFromFactors(fac);
    for (int i = 1; i < d.size(); i++) {
      if (f[d[i]] > 0) {
        flag = 1;
      }
      f[d[i]]++;
    }
  }
  if (flag == 0) {
    cout << "pairwise coprime";
  } else if (g == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}