Problem p02624 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <char cmp = '<'> bool icmp(ll a, ll b) {
  if (cmp == '>')
    return a > b;
  else
    return a < b;
}
namespace enhanced_pair {
typedef pair<ll, ll> pll;
ostream &operator<<(ostream &o, const pll a) {
  o << a.first << " " << a.second;
  return o;
}
istream &operator>>(istream &i, pll &p) {
  i >> p.first >> p.second;
  return i;
}
template <char cmp1 = '<', char cmp2 = '<'> bool pcmp(pll a, pll b) {
  return icmp<cmp1>(a.first, b.first) ||
         (a.first == b.first && icmp<cmp2>(a.second, b.second));
}
} // namespace enhanced_pair
using namespace enhanced_pair;
const ll NMAX = 1e7;
const ll MOD = 1e9 + 7;
ll lastbit(ll num) { return num & -num; }
ll binPow(ll num, ll p, ll mod = MOD) {
  if (!p)
    return 1;
  ll aux = binPow(num, p / 2);
  return aux * aux % mod * (p % 2 ? num : 1) % mod;
}
bool p[NMAX];
ll primes[NMAX], cnt;
void sieve() {
  for (ll i = 4; i < NMAX; i += 2)
    p[i] = 1;
  for (ll i = 3; i * i < NMAX; i += 2) {
    if (p[i] == 0) {
      for (ll j = i * i; j < NMAX; j += i)
        p[j] = 1;
    }
  }
  for (ll i = 2; i < 216; i++)
    if (!p[i])
      primes[cnt++] = i;
}
ll f(ll n) {
  ll d = 1, initialN = n;
  for (ll i = 0; i < cnt && primes[i] * primes[i] * primes[i] <= initialN;
       i++) {
    ll pw = 0;
    while (n % primes[i] == 0) {
      n /= primes[i];
      pw++;
    }
    d *= (pw + 1);
  }
  if (n == 1)
    return d;
  if (p[n] == 0)
    return d * 2;
  else {
    ll x = sqrt(n);
    if (x * x == n && p[x] == 0)
      return d * 3;
  }
  return d * 4;
}
int main() {
  sieve();
  ll n, ans = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    ans += i * f(i);
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <char cmp = '<'> bool icmp(ll a, ll b) {
  if (cmp == '>')
    return a > b;
  else
    return a < b;
}
namespace enhanced_pair {
typedef pair<ll, ll> pll;
ostream &operator<<(ostream &o, const pll a) {
  o << a.first << " " << a.second;
  return o;
}
istream &operator>>(istream &i, pll &p) {
  i >> p.first >> p.second;
  return i;
}
template <char cmp1 = '<', char cmp2 = '<'> bool pcmp(pll a, pll b) {
  return icmp<cmp1>(a.first, b.first) ||
         (a.first == b.first && icmp<cmp2>(a.second, b.second));
}
} // namespace enhanced_pair
using namespace enhanced_pair;
const ll NMAX = 1e7;
const ll MOD = 1e9 + 7;
ll lastbit(ll num) { return num & -num; }
ll binPow(ll num, ll p, ll mod = MOD) {
  if (!p)
    return 1;
  ll aux = binPow(num, p / 2);
  return aux * aux % mod * (p % 2 ? num : 1) % mod;
}
bool p[NMAX];
ll primes[NMAX], cnt;
void sieve() {
  for (ll i = 4; i < NMAX; i += 2)
    p[i] = 1;
  for (ll i = 3; i * i < NMAX; i += 2) {
    if (p[i] == 0) {
      for (ll j = i * i; j < NMAX; j += i)
        p[j] = 1;
    }
  }
  for (ll i = 2; i < 216; i++)
    if (!p[i])
      primes[cnt++] = i;
}
ll f(ll n) {
  ll d = 1, initialN = n;
  for (ll i = 0; i < cnt && primes[i] * primes[i] * primes[i] <= n; i++) {
    ll pw = 0;
    while (n % primes[i] == 0) {
      n /= primes[i];
      pw++;
    }
    d *= (pw + 1);
  }
  if (n == 1)
    return d;
  if (p[n] == 0)
    return d * 2;
  else {
    ll x = sqrt(n);
    if (x * x == n && p[x] == 0)
      return d * 3;
  }
  return d * 4;
}
int main() {
  sieve();
  ll n, ans = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    ans += i * f(i);
  cout << ans;
  return 0;
}