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

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

typedef long long int ll;
typedef long double ld;

//****************************************************
#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(X) X.begin(), X.end()
#define endl "\\n" // comment it for interactive questions
#define trace1(a) cerr << #a << ": " << a << endl;
#define trace2(a, b) cerr << #a << ": " << a << " " << #b << ": " << b << endl;
#define trace3(a, b, c)                                                        \\
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \\
       << endl;
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \\
       << #d << ": " << d << endl;
#define OC
#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
//*******************************************************
// Some Functions

const ll MOD = (ll)1e9 + 7; // change it for other mods

ll powmod(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b = b >> 1;
  }
  return res % MOD;
}

ll primes[10000001];
ll vis[10000001];
ll ans[10000001];

int main() {
  FAST_IO;
#ifdef STIO
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  for (ll i = 2; i <= n; i++) {
    if (vis[i] == 1) {
      continue;
    }
    for (ll j = i; j <= n; j += i) {
      primes[j] = i;
      vis[j] = 1;
    }
  }
  ll sum = 1;
  ans[1] = 1;
  for (ll i = 2; i <= n; i++) {
    ll temp = 1;
    ll cnt = 1;
    ll hoo = i;
    while (hoo % primes[i] == 0) {
      hoo /= primes[i];
      cnt++;
    }
    ans[i] = temp * cnt * ans[hoo];
    sum += i * ans[i];
  }
  cout << sum << "\\n";
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef long double ld;

//****************************************************
#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(X) X.begin(), X.end()
#define endl "\\n" // comment it for interactive questions
#define trace1(a) cerr << #a << ": " << a << endl;
#define trace2(a, b) cerr << #a << ": " << a << " " << #b << ": " << b << endl;
#define trace3(a, b, c)                                                        \\
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \\
       << endl;
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \\
       << #d << ": " << d << endl;
#define OC
#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
//*******************************************************
// Some Functions

const ll MOD = (ll)1e9 + 7; // change it for other mods

ll powmod(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b = b >> 1;
  }
  return res % MOD;
}

vll primes[10000001];
ll vis[10000001];

int main() {
  FAST_IO;
#ifdef STIO
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  for (ll i = 2; i <= n; i++) {
    if (vis[i] == 1) {
      continue;
    }
    for (ll j = i; j <= n; j += i) {
      primes[j].pb(i);
      vis[j] = 1;
    }
  }
  ll sum = 1;
  for (ll i = 2; i <= n; i++) {
    ll temp = 1;
    for (ll j = 0; j < primes[i].size(); j++) {
      ll cnt = 1;
      ll tmp = i;
      while (tmp % primes[i][j] == 0) {
        cnt++;
        tmp /= primes[i][j];
      }
      temp *= cnt;
    }
    // cout << i << " " << temp << "\\n";
    sum += i * temp;
  }
  cout << sum << "\\n";
}