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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
#define ll long long int

const int nax = 1e6 + 10;
vector<int> isp(nax, -1);
void fill() {
  isp[1] = 1;
  for (ll i = 2; i < nax; i += 2)
    isp[i] = 2;
  for (ll i = 3; i < nax; i += 2) {
    if (isp[i] == -1)
      isp[i] = i;
    for (ll j = i * i; j < nax; j += i) {
      if (isp[j] == -1)
        isp[j] = i;
    }
  }
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  fill();
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // check for pw cp
  int g = a[0];
  bool okay = true;
  vector<int> cnt(nax);
  for (int i = 0; i < n; i++) {
    cnt[a[i]]++;
  }
  for (ll i = 2; i < nax; i++) {
    int s = 0;
    for (ll j = i; j < nax; j += i) {
      s += cnt[j];
    }
    if (s > 1) {
      okay = false;
      break;
    }
  }
  if (okay) {
    cout << "pairwise coprime\\n";
    return 0;
  }
  g = a[0];
  for (int i = 0; i < n; i++) {
    g = gcd(g, a[i]);
    if (g == 1)
      break;
  }
  if (g == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
#define ll long long int

const int nax = 1e6 + 10;
vector<int> isp(nax, -1);
void fill() {
  isp[1] = 1;
  for (ll i = 2; i < nax; i += 2)
    isp[i] = 2;
  for (ll i = 3; i < nax; i += 2) {
    if (isp[i] == -1)
      isp[i] = i;
    for (ll j = i * i; j < nax; j += i) {
      if (isp[j] == -1)
        isp[j] = i;
    }
  }
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  fill();
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // check for pw cp
  int g = a[0];
  bool okay = true;
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    int temp = a[i];
    map<int, int> xy;
    xy[temp]++;
    while (isp[temp] != temp) {
      xy[isp[temp]]++;
      temp = temp / isp[temp];
      xy[temp]++;
    }
    for (auto x : xy)
      mp[x.first]++;
  }
  debug(mp);
  for (auto x : mp)
    if (x.first != 1 && x.second != 1)
      okay = false;
  if (okay) {
    cout << "pairwise coprime\\n";
    return 0;
  }
  g = a[0];
  for (int i = 0; i < n; i++) {
    g = gcd(g, a[i]);
    if (g == 1)
      break;
  }
  if (g == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
  return 0;
}