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

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, l, r) for (ll i = (l); i < (r); i++)
#define ALL(x) (x).begin(), (x).end()    // 昇順
#define RALL(x) (x).rbegin(), (x).rend() // 降順
#define pri(x) cout << (x) << "\\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\\n"
const long long mod = 1e9 + 7;
typedef long long ll;
typedef priority_queue<int> PQ;
typedef vector<long long> VL; // VL a(n);
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<char> VC;
typedef vector<VS> VSS;
typedef vector<VC> VCC;
typedef vector<VL> VLL; // VII a(n,VI(m)) n * m
typedef pair<ll, ll> PL;
typedef map<ll, ll> MP;          // MP a;
typedef vector<pair<ll, ll>> PS; // PS a(n);

template <class T, class U> bool chmax(T &a, U b) {
  if (a <= b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); }
  os << "}";
  return os;
}

// g++ -std=c++11 prac.cpp
// operator << (cout,a);
// chmin(min,a)

int main() {
  ll n, k, m, x = 0, y = 0, z = 0, cnt = 0, h = 0, w = 0, ans = 0, sum = 0,
              Max = -1, Min = 3e9 + 1;
  string s, t;
  cin >> n;
  VL a(10000010);
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      a[j]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    ans += a[i] * i;
  }
  pri(ans);
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define ALL(x) (x).begin(), (x).end()    // 昇順
#define RALL(x) (x).rbegin(), (x).rend() // 降順
#define pri(x) cout << (x) << "\\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\\n"
const long long mod = 1e9 + 7;
typedef long long ll;
typedef priority_queue<int> PQ;
typedef vector<long long> VL; // VL a(n);
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<char> VC;
typedef vector<VS> VSS;
typedef vector<VC> VCC;
typedef vector<VL> VLL; // VII a(n,VI(m)) n * m
typedef pair<ll, ll> PL;
typedef map<ll, ll> MP;          // MP a;
typedef vector<pair<ll, ll>> PS; // PS a(n);

template <class T, class U> bool chmax(T &a, U b) {
  if (a <= b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); }
  os << "}";
  return os;
}

// g++ -std=c++11 prac.cpp
// operator << (cout,a);
// chmin(min,a)

// 素因数分解 O(√n)以下
// (素因数,個数)
template <typename T> ll prime_factorization(T n) {
  // vector<pair<ll, ll> > res;
  ll an = 1;
  ll x = n;
  if (n == 1)
    return 1;
  if (n == 2 || n == 3)
    return 2;
  for (T i = 2; i * i <= n; i++) {
    ll cnt = 0;
    while (n % i == 0) {
      n /= i;
      cnt++;
    }
    an *= (cnt + 1);
  }
  if (n > 1)
    an *= 2;
  return an;
}

// 素因数分解 O(√n)以下
// (素因数,個数)
/*template<typename T>
vector<pair<T, ll> > prime_factorization(T n) {
    vector<pair<T, ll> > res;
    for (T i = 2; i*i <= n; i++) {
        ll cnt = 0;
        while (n % i == 0) {
            n /= i;
            cnt++;
        }
        if (cnt > 0) res.push_back(make_pair(i,cnt));
    }
    if (n > 1) res.push_back(make_pair(n,1));
    return res;
}*/

int main() {
  ll n, k, m, x = 1, y = 0, z = 0, cnt = 0, h = 0, w = 0, ans = 0, sum = 0,
              Max = -1, Min = 3e9 + 1;
  string s, t;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll a = prime_factorization(i);
    // pri(a);
    ans += i * a;
    // pri(ans);
  }
  pri(ans);
  return 0;
}