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

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

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cout << #x << " = " << x << endl;

template <class T> ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

template <class T> ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}

template <class T> istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}

const int MOD = 998244353, MAXN = 5e5 + 10, INF = 1e18 + 10, BASE = 37;

void solve() {
  int n;
  cin >> n;
  vec<int> f(n + 1);
  vec<int> min_p(n + 1);
  vec<int> pr;
  for (int i = 2; i <= n; ++i) {
    if (min_p[i] == 0) {
      pr.pb(i);
      min_p[i] = i;
    }
    for (int j = 0; j < $(pr) && pr[j] <= min_p[i] && pr[j] * i <= n; ++j) {
      min_p[pr[j] * i] = pr[j];
    }
  }

  int s = 1;
  for (int i = 2; i <= n; ++i) {
    if (min_p[i] == i) {
      s += 2 * i;
      f[i] = 2;
    } else {
      int el = 1;
      int x = i;
      int p = min_p[x];
      int k = 0;
      while (min_p[x] == p) {
        el *= min_p[x];
        x /= min_p[x];
        k++;
      }
      if (x == 1) {
        s += i * (k + 1);
        f[i] = k + 1;
      } else {
        f[i] = f[i / el] * f[el];
        s += i * f[i];
      }
    }
  }

  cout << s << endl;
}

Error: 0

Fixed submission:

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

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cout << #x << " = " << x << endl;

template <class T> ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

template <class T> ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}

template <class T> istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}

const int MOD = 998244353, MAXN = 5e5 + 10, INF = 1e18 + 10, BASE = 37;

void solve() {
  int n;
  cin >> n;
  vec<int> f(2 * n + 1);
  vec<int> min_p(2 * n + 1);
  vec<int> pr;
  for (int i = 2; i < n; ++i) {
    if (min_p[i] == 0) {
      pr.pb(i);
      min_p[i] = i;
    }
    for (int j = 0; j < $(pr) && pr[j] <= min_p[i] && pr[j] * i <= n; ++j) {
      min_p[pr[j] * i] = pr[j];
    }
  }

  int s = 1;
  for (int i = 2; i <= n; ++i) {
    if (min_p[i] == i) {
      s += 2 * i;
      f[i] = 2;
    } else {
      int el = 1;
      int x = i;
      int p = min_p[x];
      int k = 0;
      while (min_p[x] == p) {
        el *= min_p[x];
        x /= min_p[x];
        k++;
      }
      if (x == 1) {
        s += i * (k + 1);
        f[i] = k + 1;
      } else {
        f[i] = f[i / el] * f[el];
        s += i * f[i];
      }
    }
  }

  cout << s << endl;
}