Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define T top()
#define E end()
#define PS push
#define Q queue
#define PP pop()
#define L0 (LL)0
#define V vector
#define ST stack
#define FI first
#define MAX 4e18
#define MIN -MAX
#define DQ deque
#define RE rend()
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define SE second
#define DL double
#define G getline
#define IN insert
#define endl "\\n"
#define EM empty()
#define EE emplace
#define RB rbegin()
#define MD 1000000007
#define PSB push_back
#define PI acos(-1.00)
#define PPB pop_back()
#define PSF push_front
#define ub upper_bound
#define lb lower_bound
#define PPF pop_front()
#define EB emplace_back
#define LL long long int
#define PQ priority_queue
#define N cout << "NO" << endl
#define Y cout << "YES" << endl
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#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)
#define UOSMOY                                                                 \\
  v.C;                                                                         \\
  w.C;                                                                         \\
  ww.C;                                                                        \\
  uu.C;                                                                        \\
  uo.C;                                                                        \\
  vo.C;                                                                        \\
  vu.C;                                                                        \\
  ab.C;                                                                        \\
  bc.C;                                                                        \\
  cc.C;                                                                        \\
  uuu.C;                                                                       \\
  a.C;                                                                         \\
  b.C;                                                                         \\
  c.C;                                                                         \\
  u.C
V<LL> v;
V<LL> w, ab, bc;
set<LL> uo;
map<LL, LL> ww, cc, u;
V<pair<LL, LL>> uu;
map<LL, V<LL>> uuu;
map<pair<LL, LL>, LL> vo;
map<pair<LL, LL>, LL> vu;
LL dxx[] = {-1, 0, 1, 0};
LL dyy[] = {0, 1, 0, -1};
LL dx[] = {2, 1, -1, -2, -2, -1, 1, 2};
LL dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
LL lpf[1000001], mobius[1000001], fre[1000001] = {0};
LL br[1000001];

void least_prime_factor() {
  for (long long int i = 2; i < (1000001); i++)
    if (!lpf[i])

      for (long long int j = i; j < 1000001; j += i)
        if (!lpf[j])
          lpf[j] = i;
}
void Mobius() {
  for (long long int i = 1; i < 1000001; i++) {
    if (i == 1)
      mobius[i] = 1;
    else {
      if (lpf[i / lpf[i]] == lpf[i])
        mobius[i] = 0;
      else
        mobius[i] = -1 * mobius[i / lpf[i]];
    }
  }
}

long long int gcd_pairs(long long int a[], long long int n) {

  long long int maxi = 0;

  for (long long int i = 0; i < n; i++) {
    fre[a[i]]++;
    maxi = max(a[i], maxi);
  }

  least_prime_factor();
  Mobius();
  long long int ans = 0;
  for (long long int i = 1; i <= maxi; i++) {
    if (!mobius[i])
      continue;

    long long int temp = 0;
    for (long long int j = i; j <= maxi; j += i)
      temp += fre[j];

    ans += temp * (temp - 1) / 2 * mobius[i];
  }

  return ans;
}
int main() {
  //  BRR;
  //  file;
  FAST;
  char ch;
  string a, b, c;
  DL x, y, d, e, f, g, h;
  LL n, m, i, j, k, p, q, o, l, s, tt = 1, t, z, aa, r = 0;
  //    cin>>tt;
  while (tt--) {
    UOSMOY;
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> br[i];
    }
    k = br[0];
    for (i = 1; i < n; i++) {
      k = __gcd(k, br[i]);
    }
    if (k == 1) {
      l = gcd_pairs(br, n);
      p = n;
      p *= (n - 1);
      p /= 2;
      if (l == p) {
        cout << "pairwise coprime" << endl;
      } else {
        cout << "setwise coprime" << endl;
      }
    } else {
      cout << "not coprime" << endl;
    }
  }
  return 0;
  // IN
  // AL
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define T top()
#define E end()
#define PS push
#define Q queue
#define PP pop()
#define L0 (LL)0
#define V vector
#define ST stack
#define FI first
#define MAX 4e18
#define MIN -MAX
#define DQ deque
#define RE rend()
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define SE second
#define DL double
#define G getline
#define IN insert
#define endl "\\n"
#define EM empty()
#define EE emplace
#define RB rbegin()
#define MD 1000000007
#define PSB push_back
#define PI acos(-1.00)
#define PPB pop_back()
#define PSF push_front
#define ub upper_bound
#define lb lower_bound
#define PPF pop_front()
#define EB emplace_back
#define LL long long int
#define PQ priority_queue
#define N cout << "NO" << endl
#define Y cout << "YES" << endl
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#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)
#define UOSMOY                                                                 \\
  v.C;                                                                         \\
  w.C;                                                                         \\
  ww.C;                                                                        \\
  uu.C;                                                                        \\
  uo.C;                                                                        \\
  vo.C;                                                                        \\
  vu.C;                                                                        \\
  ab.C;                                                                        \\
  bc.C;                                                                        \\
  cc.C;                                                                        \\
  uuu.C;                                                                       \\
  a.C;                                                                         \\
  b.C;                                                                         \\
  c.C;                                                                         \\
  u.C
V<LL> v;
V<LL> w, ab, bc;
set<LL> uo;
map<LL, LL> ww, cc, u;
V<pair<LL, LL>> uu;
map<LL, V<LL>> uuu;
map<pair<LL, LL>, LL> vo;
map<pair<LL, LL>, LL> vu;
LL dxx[] = {-1, 0, 1, 0};
LL dyy[] = {0, 1, 0, -1};
LL dx[] = {2, 1, -1, -2, -2, -1, 1, 2};
LL dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
map<LL, LL> lpf, mobius, fre;
LL br[1000001];

void least_prime_factor() {
  for (long long int i = 2; i < sqrt(1000001); i++)
    if (!lpf[i])

      for (long long int j = i * i; j < 1000001; j += i)
        if (!lpf[j])
          lpf[j] = i;
}
void Mobius() {
  for (long long int i = 1; i < 1000001; i++) {
    if (i == 1)
      mobius[i] = 1;
    else {
      if (lpf[i / lpf[i]] == lpf[i])
        mobius[i] = 0;
      else
        mobius[i] = -1 * mobius[i / lpf[i]];
    }
  }
}

long long int gcd_pairs(long long int a[], long long int n) {

  long long int maxi = 0;

  for (long long int i = 0; i < n; i++) {
    fre[a[i]]++;
    maxi = max(a[i], maxi);
  }

  least_prime_factor();
  Mobius();
  long long int ans = 0;
  for (long long int i = 1; i <= maxi; i++) {
    if (!mobius[i])
      continue;

    long long int temp = 0;
    for (long long int j = i; j <= maxi; j += i)
      temp += fre[j];

    ans += temp * (temp - 1) / 2 * mobius[i];
  }

  return ans;
}
int main() {
  //  BRR;
  //  file;
  FAST;
  char ch;
  string a, b, c;
  DL x, y, d, e, f, g, h;
  LL n, m, i, j, k, p, q, o, l, s, tt = 1, t, z, aa, r = 0;
  //    cin>>tt;
  while (tt--) {
    UOSMOY;
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> br[i];
    }
    k = br[0];
    for (i = 1; i < n; i++) {
      k = __gcd(k, br[i]);
    }
    if (k == 1) {
      l = gcd_pairs(br, n);
      p = n;
      p *= (n - 1);
      p /= 2;
      if (l == p) {
        cout << "pairwise coprime" << endl;
      } else {
        cout << "setwise coprime" << endl;
      }
    } else {
      cout << "not coprime" << endl;
    }
  }
  return 0;
  // IN
  // AL
}