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

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

#define ll long long
#define MAXN 10000005

// stores smallest prime factor for every number
int spf[MAXN];

void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<int> getFactorization(int x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

int main(int argc, char const *argv[]) {
  // precalculating Smallest Prime Factor
  sieve();
  int n;
  cin >> n;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    vector<int> p = getFactorization(i);
    ll temp = 1;
    int f = i;
    for (int x : p) {
      int cnt = 0;
      while (f % x == 0) {
        f = f / x;
        cnt++;
      }
      temp = temp * (ll)(cnt + 1);
    }
    ans += (ll)i * temp;
  }
  cout << ans;

  return 0;
}

Error: TLE

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;

#define double long double
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 1e7 + 5;
const int MOD = 1e9 + 7;

bool prime[N];
vector<int> pr;

void SieveOfEratosthenes(int maxn) {
  pr.clear();

  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= maxn; p++) {
    if (prime[p] == true) {
      for (int i = p * 2; i <= maxn; i += p)
        prime[i] = false;
    }
  }
  for (int p = 2; p <= maxn; p++)
    if (prime[p]) {
      pr.push_back(p);
    }
}

int32_t main() {
  IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    int n;
    cin >> n;
    SieveOfEratosthenes(n + 1);
    ll ans = 0;
    rep(i, 1, n) {
      int x = i;
      int temp = 1;
      for (int p = 2; p * p <= x; p++) {
        int cnt = 0;
        if (prime[p] == false)
          continue;
        while (x % p == 0) {
          cnt++;
          x = x / p;
        }
        if (cnt > 0) {
          cnt++;
          temp = temp * cnt;
        }
      }
      if (x > 1 && prime[x] == true)
        temp = temp * 2;
      // deb1(temp);
      ans += (ll)i * (ll)temp;
    }
    cout << ans;
  }
}