Problem p02624 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  srand((unsigned)time(NULL));
  ll N;
  cin >> N;
  ll ans = 0LL;
  for (int i = 1; i <= N; i++) {
    ll num = N / i;
    ll x = (i + num * i) * num / 2;
    ans += x;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\\n"

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = -1;
    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;
  }
};

template <typename T> ll lcm(T a, T b) {
  return (ll)a * (ll)b / (ll)__gcd(a, b);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  srand((unsigned)time(NULL));
  ll N;
  cin >> N;
  vll dp(N + 1, 0LL);
  Sieve s(N);
  ll NN = s.primes.size();
  dp.at(1) = 1;
  for (int i = 0; i < NN; i++) {
    ll p = s.primes.at(i);
    ll num = p;
    ll res = 2;
    while (num <= N) {
      dp.at(num) = res;
      res++;
      num *= p;
    }
  }
  for (ll i = 6; i <= N; i++) {
    if (dp.at(i) != 0)
      continue;
    for (ll j = 2; j <= i; j++) {
      if (i % j == 0) {
        ll jj = j;
        while (i % jj == 0) {
          jj *= j;
        }
        jj /= j;
        dp.at(i) = dp.at(jj) * dp.at(i / jj);
        break;
      }
    }
  }
  ll ans = 0LL;
  for (ll i = 1; i <= N; i++) {
    ans += i * dp.at(i);
  }
  cout << ans << endl;
}