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

#pragma GCC optimize("03")
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define ll long long int
using namespace std;

#define N 1000001
const ll mod = 1000000007;

int lpf[N], mobius[N];

void least_prime_factor() {
  for (int i = 2; i < N; i++)
    if (!lpf[i])
      for (int j = i; j < N; j += i)
        if (!lpf[j])
          lpf[j] = i;
}

void Mobius() {
  for (int i = 1; i < N; 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]];
    }
  }
}

int gcd_pairs(int a[], int n) {
  int maxi = 0;

  int fre[N] = {0};

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

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

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

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

  return ans;
}

void solve() {
  least_prime_factor();
  Mobius();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int s = a[0];
  for (int i = 1; i < n; i++)
    s = __gcd(s, a[i]);
  if (gcd_pairs(a, n) == n * (n - 1) / 2) {
    cout << "pairwise coprime";
  } else if (s == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin >> t;
  t = 1;
  while (t--) {
    solve();
    cout << "\\n";
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define ll long long int
using namespace std;

#define N 100050
const ll mod = 1000000007;

int lpf[N], mobius[N];

void least_prime_factor() {
  for (int i = 2; i < N; i++)
    if (!lpf[i])
      for (int j = i; j < N; j += i)
        if (!lpf[j])
          lpf[j] = i;
}

void Mobius() {
  for (int i = 1; i < N; 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]];
    }
  }
}

int gcd_pairs(int a[], int n) {
  int maxi = 0;

  int fre[N] = {0};

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

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

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

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

  return ans;
}

void solve() {
  least_prime_factor();
  Mobius();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int s = a[0];
  for (int i = 1; i < n; i++)
    s = __gcd(s, a[i]);
  if (gcd_pairs(a, n) == n * (n - 1) / 2) {
    cout << "pairwise coprime";
  } else if (s == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  // cin >> t;
  t = 1;
  while (t--) {
    solve();
    cout << "\\n";
  }
  return 0;
}