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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

ll findMaxGCD(vector<ll> arr, ll n) {

  ll high = 0;
  for (ll i = 0; i < n; i++)
    high = max(high, arr[i]);

  ll count[high + 1] = {0};
  for (ll i = 0; i < n; i++)
    count[arr[i]]++;

  ll counter = 0;

  for (ll i = high; i >= 1; i--) {
    ll j = i;
    counter = 0;

    while (j <= high) {

      if (count[j] >= 2)
        return j;

      else if (count[j] == 1)
        counter++;

      j += i;

      if (counter == 2)
        return i;
    }
  }
}

ll findGCD(vector<ll> arr, ll n) {
  ll result = arr[0];
  for (ll i = 1; i < n; i++) {
    result = __gcd(arr[i], result);

    if (result == 1) {
      return 1;
    }
  }
  return result;
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  for (ll i = 0; i < n; ++i)
    cin >> vec[i];

  bool pairwise = findMaxGCD(vec, n) == 1 ? true : false;
  // cout<<gcd_pairs(vec,n);
  bool setwise = findGCD(vec, n) == 1 ? true : false;
  if (pairwise)
    cout << "pairwise coprime";
  else if (setwise)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll q;
  q = 1;
  // cin>>q;
  while (q--)
    solve();

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

#define N 100007

ll lpf[N], mobius[N];

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

void Mobius() {
  for (ll 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]];
    }
  }
}

ll gcd_pairs(vector<ll> a, ll n) {

  ll maxi = 0;

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

  least_prime_factor();
  Mobius();

  ll ans = 0;

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

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

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

  return ans;
}

ll findGCD(vector<ll> arr, ll n) {
  ll result = arr[0];
  for (ll i = 1; i < n; i++) {
    result = __gcd(arr[i], result);

    if (result == 1) {
      return 1;
    }
  }
  return result;
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  for (ll i = 0; i < n; ++i)
    cin >> vec[i];

  bool pairwise = gcd_pairs(vec, n) == n * (n - 1) / 2 ? true : false;
  // cout<<gcd_pairs(vec,n);
  bool setwise = findGCD(vec, n) == 1 ? true : false;
  if (pairwise)
    cout << "pairwise coprime";
  else if (setwise)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll q;
  q = 1;
  // cin>>q;
  while (q--)
    solve();

  return 0;
}