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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD 998244353
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, ll> &firstElof,
                       const pair<double, ll> &secondElof) {
  return firstElof.second < secondElof.second;
}
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0;
    while (N % a == 0) {
      ++ex;
      N /= a;
    }
    res.push_back(make_pair(a, ex));
  }
  if (N != 1)
    res.push_back(make_pair(N, 1));
  return res;
}

int main() {
  ll n, ans = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    for (ll j = i; j <= n; j += i) {
      ans += j;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD 998244353
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, ll> &firstElof,
                       const pair<double, ll> &secondElof) {
  return firstElof.second < secondElof.second;
}
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0;
    while (N % a == 0) {
      ++ex;
      N /= a;
    }
    res.push_back(make_pair(a, ex));
  }
  if (N != 1)
    res.push_back(make_pair(N, 1));
  return res;
}

int main() {
  long long N;
  cin >> N;
  const auto &pf = prime_factorize(N);
  long long res = 1;
  ll ans = 0;
  for (ll i = 1; i <= N; i++) {
    const auto &pf = prime_factorize(i);
    long long res = 1;
    for (auto p : pf)
      res *= p.second + 1;
    ans += i * res;
  }
  cout << ans << endl;
}