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

/* HARE KRSNA HARE KRSNA KRSNA KRSNA HARE HARE
    HARE RAMA HARE RAMA RAMA RAMA HARE HARE  */

#include <bits/stdc++.h>
using namespace std;

#define nl "\\n"
#define ff first
#define ss second
#define pp pop_back
#define pf pop_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) x.begin(), x.end()
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define loop(i, s, n) for (ll i = s; i < n; i++)
#define rloop(i, s, n) for (ll i = s; i >= n; i--)
#define fill(a, value) memset(a, value, sizeof(a))

#define debug2(a)                                                              \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << nl;
#define debug4(a)                                                              \\
  for (auto x : a)                                                             \\
    cout << x.ff << " " << x.ss << nl;
#define debug1(a, n)                                                           \\
  loop(i, 0, n) cout << a[i] << " ";                                           \\
  cout << nl;
#define debug3(a, n, m)                                                        \\
  loop(i, 0, n) {                                                              \\
    loop(j, 0, m) cout << a[i][j] << " ";                                      \\
    cout << nl;                                                                \\
  }

typedef long long ll;
typedef long double ld;

typedef stack<ll> sll;
typedef queue<ll> qll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef pair<ll, pll> ppll;
typedef vector<vector<ll>> vvll;
typedef priority_queue<ll> pqll;

const ll mod = 1000000007;  // 998244353;
const ld pi = acos((ld)-1); // 3.1415926535;

ll num_generator() { return rand() % 1000; }
void array_generator(ll a[], ll n) { loop(i, 0, n) a[i] = rand() % 100; }

const ll mxn = 10000001;
bool prime[mxn];
ll spf[mxn];

void seive() {
  prime[1] = true;
  loop(i, 2, mxn) {
    if (!prime[i]) {
      spf[i] = i;
      for (ll j = i * i; j < mxn; j += i) {
        if (!prime[j]) {
          prime[j] = true;
          spf[j] = i;
        }
      }
    }
  }
}

void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  seive();
  loop(i, 1, n + 1) {
    if (i == 1)
      ans++;
    else {
      ll div = 1, d = spf[i], num = i;
      map<ll, ll> M;
      while (num != 1) {
        num /= d;
        M[d]++;
        d = spf[num];
      }
      for (auto m : M) {
        div *= (m.ss + 1);
      }
      ans += i * div;
    }
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // test
  solve();
  return 0;
}

Error: TLE

Fixed submission:

/* HARE KRSNA HARE KRSNA KRSNA KRSNA HARE HARE
    HARE RAMA HARE RAMA RAMA RAMA HARE HARE  */

#include <bits/stdc++.h>
using namespace std;

#define nl "\\n"
#define ff first
#define ss second
#define pp pop_back
#define pf pop_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(x) x.begin(), x.end()
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define loop(i, s, n) for (ll i = s; i < n; i++)
#define rloop(i, s, n) for (ll i = s; i >= n; i--)
#define fill(a, value) memset(a, value, sizeof(a))

#define debug2(a)                                                              \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << nl;
#define debug4(a)                                                              \\
  for (auto x : a)                                                             \\
    cout << x.ff << " " << x.ss << nl;
#define debug1(a, n)                                                           \\
  loop(i, 0, n) cout << a[i] << " ";                                           \\
  cout << nl;
#define debug3(a, n, m)                                                        \\
  loop(i, 0, n) {                                                              \\
    loop(j, 0, m) cout << a[i][j] << " ";                                      \\
    cout << nl;                                                                \\
  }

typedef long long ll;
typedef long double ld;

typedef stack<ll> sll;
typedef queue<ll> qll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef pair<ll, pll> ppll;
typedef vector<vector<ll>> vvll;
typedef priority_queue<ll> pqll;

const ll mod = 1000000007;  // 998244353;
const ld pi = acos((ld)-1); // 3.1415926535;

ll num_generator() { return rand() % 1000; }
void array_generator(ll a[], ll n) { loop(i, 0, n) a[i] = rand() % 100; }

void solve() {
  ll n;
  cin >> n;
  ll sum = 0;
  loop(i, 1, n + 1) {
    set<ll> S;
    loop(j, 1, sqrt(i) + 1) {
      if (i % j == 0) {
        S.insert(j);
        if (i % (i / j) == 0)
          S.insert(i / j);
      }
    }
    sum += i * S.size();
  }
  cout << sum;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // test
  solve();
  return 0;
}