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

/*
    हॉंजी । की हाल चाल?
    बड़ीया सब?
*/

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii unordered_map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define EPS 1e-9

void gogeta() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int MAXN = 1000001;

vector<int> spf(MAXN);

void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

int getFactorization(int x) {
  mii ret;
  while (x != 1) {
    ret[(spf[x])]++;
    x = x / spf[x];
  }
  int ans = 1;
  for (auto x : ret) {
    ans *= (x.ss + 1);
  }
  return ans;
}

int32_t main() {
  gogeta();
  int n;
  cin >> n;
  sieve();
  int ans = 0;
  for (int i = 1; i < n; i++) {
    ans += getFactorization(i);
    // cout << fac[i] << endl;
  }
  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

/*
    हॉंजी । की हाल चाल?
    बड़ीया सब?
*/

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii unordered_map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define EPS 1e-9

void gogeta() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  gogeta();
  int n;
  cin >> n;
  vector<int> fac(n, 0);

  for (int i = 1; i <= n; i++) {
    int count = 0;
    int j;
    for (j = 1; j * j <= i; j++) {
      if (i % j == 0)
        count++;
    }
    j--;
    if (j * j != i)
      count = 2 * count;
    else
      count = 2 * count - 1;
    fac[i] = count;
  }

  int ans = 0;
  for (int i = 1; i < n; i++) {
    ans += fac[i];
    // cout << fac[i] << endl;
  }
  cout << ans << endl;
}