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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<int, pii> viii;
typedef vector<pll> vll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vector<vl>> vvvl;
const ll MAXN = 1000500;
vector<ll> spf(MAXN + 1);
ll gcd1(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd1(b, a % b);
}
void sieve() {
  for (ll i = 1; i < MAXN; i++)
    spf[i] = i;
  for (ll i = 2; i < MAXN; i += 2)
    spf[i] = 2;

  for (ll i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (ll j = i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
vector<ll> getFactorization(ll x) {
  vector<ll> ret;
  ll count = 0;
  while (x != 1) {
    if (count == 0 || ret[count - 1] != spf[x]) {
      ret.push_back(spf[x]);
      count++;
    }
    x = x / spf[x];
  }
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, i, j, k;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> mark(1e6 + 500, 0);
  ll flag = 1;
  sieve();
  for (i = 0; i < n; i++) {
    vl p = getFactorization(a[i]);
    ll k = p.size();
    for (j = 0; j < k; j++) {
      if (mark[p[j]] == 0)
        mark[p[j]] = 1;
      else {
        flag = 0;
      }
    }
  }
  if (flag)
    cout << "pairwise coprime";
  else {
    ll gcd = a[0];
    for (i = 1; i < n; i++) {
      gcd = gcd1(gcd, a[i]);
    }
    if (gcd == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
  return 0;
}


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


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<int, pii> viii;
typedef vector<pll> vll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vector<vl>> vvvl;
const ll MAXN = 10005000;
vector<ll> spf(MAXN + 1);
ll gcd1(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd1(b, a % b);
}
void sieve() {
  for (ll i = 1; i < MAXN; i++)
    spf[i] = i;
  for (ll i = 2; i < MAXN; i += 2)
    spf[i] = 2;

  for (ll i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (ll j = i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

vector<ll> getFactorization(ll x) {
  vector<ll> ret;
  ll count = 0;
  while (x != 1) {
    if (count == 0 || ret[count - 1] != spf[x]) {
      ret.push_back(spf[x]);
      count++;
    }
    x = x / spf[x];
  }
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  sieve();
  ll n, m, i, j, k;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> mark(n + 1, 0);
  ll flag = 1;
  for (i = 0; i < n; i++) {
    // if(a[i]==1)continue;
    vl p = getFactorization(a[i]);
    // cout<<a[i]<<endl;
    // for(j=0;j<p.size();j++)cout<<p[j]<<" ";cout<<endl;
    ll k = p.size();
    for (j = 0; j < k; j++) {
      if (mark[p[j]] == 0)
        mark[p[j]] = 1;
      else {
        flag = 0;
      }
    }
  }
  if (flag == 1)
    cout << "pairwise coprime";
  else {
    ll gcd = a[0];
    for (i = 1; i < n; i++) {
      gcd = gcd1(gcd, a[i]);
    }
    if (gcd == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
  return 0;
}