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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }
  bool isPrime(int x) { return f[x] == x; }
  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<P> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<P> res(1, P(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};

int main() {
  // ll n;
  // ll ans = 0;
  // cin >> n;
  // vector<ll> a(n);
  // rep(i,n){
  //     // for (int j=i;j<=n;j += i+1)a[j]+=1;
  //     ll r = n/(i+1);
  //     ans += (i+1)*r*(1+r)/2;
  // }
  // rep(i,n)ans += (i+1)*a[i];
  // cout << ans << endl;

  int n;
  cin >> n;
  ll ans = 0;
  Sieve S(n + 1);
  for (int i = 1; i <= n; i++) {
    ll temp = 1;
    for (auto p : S.factor(i)) {
      temp *= p.second + 1;
    }
    // cout << i << " " << temp << endl;
    ans += i * temp;
  }

  cout << ans << endl;
}

struct Linear_Sieve {
  int n;
  vector<int> lp, primes;
  // lp[i]...the least prime of i. primes...prime numbers
  Linear_Sieve(int n = 1) : n(n), lp(n + 1) {
    for (int i = 2; i < n + 1; i++) {
      if (lp[i] == 0) {
        primes.push_back(i);
        lp[i] = i;
      }
      for (int j = 0;
           j < primes.size() && primes[j] <= lp[i] && i * primes[j] < n + 1;
           ++j)
        lp[i * primes[j]] = primes[j];
    }
  }
  bool isPrime(int x) { return lp[x] == x; }

  vector<int> factorList(int x) {
    // make list which contains prime factors of x. Ans it toralates multipled
    // components.
    vector<int> res;
    while (x != 1) {
      res.push_back(lp[x]);
      x /= lp[x];
    }
    return res;
  }

  vector<pair<int, int>> factor(int x) {
    // make list of pairs which couple prime factor and multiplicity.
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<int, int>> res(1, pair<int, int>(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (int i = 2; i <= n; i++) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }

  bool isPrime(int x) { return f[x] == x; }
  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<pair<int, int>> factor(int x) {

    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<int, int>> res(1, pair<int, int>(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};

int main() {
  // ll n;
  // ll ans = 0;
  // cin >> n;
  // vector<ll> a(n);
  // rep(i,n){
  //     // for (int j=i;j<=n;j += i+1)a[j]+=1;
  //     ll r = n/(i+1);
  //     ans += (i+1)*r*(1+r)/2;
  // }
  // rep(i,n)ans += (i+1)*a[i];
  // cout << ans << endl;

  int n;
  cin >> n;
  ll ans = 0;
  Sieve S(n + 1);
  for (int i = 1; i <= n; i++) {
    ll temp = 1;
    for (auto p : S.factor(i)) {
      temp *= p.second + 1;
    }
    // cout << i << " " << temp << endl;
    ans += i * temp;
  }

  cout << ans << endl;
}