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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define mp make_pair
#define v vector
#define inp_push(no, v)                                                        \\
  ll no;                                                                       \\
  cin >> no;                                                                   \\
  v.push_back(no);
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define UN(v)                                                                  \\
  sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end())
#define N 200005
#define mod 1000000009
#define INF INT64_MAX

//////////////////////////IMPORTANT
///FUNCTIONS/////////////////////////////////////////////

/* Guidlines berfore submitting solution
1. Check N value before submitting
2. Check all constant kind of values you are using. Might give WA. Think
3. Check mod value
*/

v<ll> fact(N);
v<ll> prime_check(1e7 + 1);
v<ll> primes;

long long powmod(long long x, long long y, long long m) {
  long long res = 1LL;
  while (y) {
    if (y & 1)
      res = (res * x) % m;
    x = (x * x) % m;
    y /= 2;
  }
  return res;
}

void init_fact() {
  fact[0] = 1;
  for (ll i = 1; i < N; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
}

ll C(ll n, ll r) {
  if (n < r)
    return 0;
  ll temp = fact[n] * powmod(fact[r], mod - 2, mod) % mod;
  temp *= powmod(fact[n - r], mod - 2, mod);
  temp %= mod;
  return temp;
}

void Sieve() {
  for (ll i = 2; i <= 1e7; i++) {
    if (prime_check[i])
      continue;
    primes.pb(i);
    for (ll j = 2 * i; j <= 1e7; j += i)
      prime_check[j] = i;
  }
}
ll phi(ll n) {
  ll i, res = n;
  for (i = 2; i * i <= n; i++)
    if (n % i == 0) {
      while (n % i == 0)
        n /= i;
      res -= res / i;
    }
  if (n > 1)
    res -= res / n;
  return res;
}

/////////////////////////////////////START CODE
///HERE/////////////////////////////////////////////

ll n;
v<ll> dp(1e7 + 1);
v<ll> dp1(1e7 + 1);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // init_fact();
  Sieve();
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ll m = prime_check[i];
    if (i == 1)
      dp[i] = 1, dp1[i] = 0;
    else if (m == 0)
      dp[i] = 2, dp1[i] = 1;
    else if ((i / m) % m != 0) {
      dp[i] = dp[i / m] * 2, dp1[i] = 1;
    } else {
      dp[i] = (dp[i / m] / (dp1[i / m] + 1)) * (dp1[i / m] + 2);
      dp1[i] = dp1[i / m] + 1;
    }
    // cout << dp[i] << " ";
    ans += i * dp[i];
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define mp make_pair
#define v vector
#define inp_push(no, v)                                                        \\
  ll no;                                                                       \\
  cin >> no;                                                                   \\
  v.push_back(no);
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define UN(v)                                                                  \\
  sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end())
#define N 200005
#define mod 1000000009
#define INF INT64_MAX

//////////////////////////IMPORTANT
///FUNCTIONS/////////////////////////////////////////////

/* Guidlines berfore submitting solution
1. Check N value before submitting
2. Check all constant kind of values you are using. Might give WA. Think
3. Check mod value
*/

v<ll> fact(N);
v<ll> prime_check(1e7 + 1);
v<ll> primes;

long long powmod(long long x, long long y, long long m) {
  long long res = 1LL;
  while (y) {
    if (y & 1)
      res = (res * x) % m;
    x = (x * x) % m;
    y /= 2;
  }
  return res;
}

void init_fact() {
  fact[0] = 1;
  for (ll i = 1; i < N; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
}

ll C(ll n, ll r) {
  if (n < r)
    return 0;
  ll temp = fact[n] * powmod(fact[r], mod - 2, mod) % mod;
  temp *= powmod(fact[n - r], mod - 2, mod);
  temp %= mod;
  return temp;
}

void Sieve() {
  for (ll i = 2; i <= 1e7; i++) {
    if (prime_check[i])
      continue;
    primes.pb(i);
    for (ll j = 2 * i; j <= 1e7; j += i)
      prime_check[j] = i;
  }
}
ll phi(ll n) {
  ll i, res = n;
  for (i = 2; i * i <= n; i++)
    if (n % i == 0) {
      while (n % i == 0)
        n /= i;
      res -= res / i;
    }
  if (n > 1)
    res -= res / n;
  return res;
}

/////////////////////////////////////START CODE
///HERE/////////////////////////////////////////////

ll n;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // init_fact();
  Sieve();
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ll ans1 = 1, t1 = i;
    for (ll j = 0; j < primes.size(); j++) {
      if (primes[j] > sqrt(t1))
        break;
      ll t2 = 0;
      while (t1 % primes[j] == 0)
        t2++, t1 /= primes[j];
      ans1 *= (t2 + 1);
    }
    if (t1 != 1)
      ans1 *= 2;
    ans += (ans1 * i);
    // cout << ans << " ";
  }
  cout << ans << "\\n";
}