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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const int mod = 1000000007;
#define ll long long
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
int _, a, b, n, k, m;
#define MAXN 10000001
map<int, int> map1;
int spf[MAXN];
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)
    spf[i] = i;
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i) {
        if (spf[j] == j)
          spf[j] = i;
      }
    }
  }
}
ll getFactorization(ll x) {
  vector<ll> ret;
  ll ans = 1;
  while (x != 1) {

    ret.pb(spf[x]);
    map1[spf[x]]++;
    x = x / spf[x];
  }
  for (auto itr = map1.begin(); itr != map1.end(); itr++) {
    ll d = (itr->second) + 1;
    ans = ans * d;
  }
  map1.clear();
  return ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, z = 1, ans = 1, sum = 0, f;
  cin >> n;
  sieve();
  map<ll, ll> map1;
  for (ll i = 1; i <= n; i++) {
    f = getFactorization(i);
    sum += z * f;
    z++;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const int mod = 1000000007;
#define ll long long
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head
int _, a, b, n, k, m;
#define MAXN 10000001
map<int, int> map1;
int spf[MAXN];
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)
    spf[i] = i;
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i) {
        if (spf[j] == j)
          spf[j] = i;
      }
    }
  }
}
std::vector<ll> getFactorization(ll x) {
  vector<ll> ret;
  while (x != 1) {

    ret.pb(spf[x]);
    map1[spf[x]]++;
    x = x / spf[x];
  }
  return ret;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, z = 1, ans = 1, sum = 0;
  cin >> n;
  sieve();
  map<ll, ll> map1;
  for (ll i = 1; i <= n; i++) {
    std::vector<ll> arr = getFactorization(i);
    for (ll i = 0; i < arr.size(); i++) {
      map1[arr[i]]++;
    }
    for (auto itr = map1.begin(); itr != map1.end(); itr++) {
      ll d = (itr->second) + 1;
      ans = ans * (d);
    }
    sum += z * ans;
    z++;
    map1.clear();
    ans = 1;
  }
  cout << sum << endl;
}