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

#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;
// printf("%.10f\\n", n);

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll modpow(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
      res %= mod;
    }
    x = x * x % mod;
    y /= 2;
  }
  return res;
}

ll prime_factor(ll n) {
  ll gh = 1;
  for (ll i = 2; i * i <= n; i++) {
    ll ok = 1;
    while (n % i == 0) {
      ok++;
      n /= i;
    }
    gh *= ok;
  }
  if (n != 1) {
    gh *= 2;
  }
  return gh;
}

ll test[12345678], aa[12345678];
signed main() {
  ll n, m, k, q, a = -1, b = -1, c = 0, cnt = 1, ans = 0;
  string s, t;
  cin >> n;
  if (n >= 1000000) {
    ans = 7235062807366;
    cnt = 1000001;
  }
  if (n >= 1500000) {
    ans = 16734924755096;
    cnt = 1500001;
  }
  if (n >= 2000000) {
    ans = 30326413365285;
    cnt = 2000001;
  }
  if (n >= 2500000) {
    ans = 48081994935671;
    cnt = 2500001;
  }
  if (n >= 3000000) {
    ans = 70058785460280;
    cnt = 3000001;
  }
  if (n >= 3500000) {
    ans = 96301520878991;
    cnt = 3500001;
  }
  if (n >= 4000000) {
    ans = 126850549017804;
    cnt = 4000001;
  }
  if (n >= 4500000) {
    ans = 161737467121483;
    cnt = 4500001;
  }
  if (n >= 5000000) {
    ans = 200992441246692;
    cnt = 5000001;
  }
  if (n >= 5500000) {
    ans = 244642548171917;
    cnt = 5500001;
  }
  if (n >= 6000000) {
    ans = 292711324081941;
    cnt = 6000001;
  }
  if (n >= 6500000) {
    ans = 345219904513529;
    cnt = 6500001;
  }
  if (n >= 7000000) {
    ans = 402188826344049;
    cnt = 7000001;
  }
  if (n >= 7500000) {
    ans = 463635895079489;
    cnt = 7500001;
  }
  if (n >= 8000000) {
    ans = 529581632274134;
    cnt = 8000001;
  }
  if (n >= 8500000) {
    ans = 600036645526326;
    cnt = 8500001;
  }
  if (n >= 9000000) {
    ans = 675021515919670;
    cnt = 9000001;
  }
  if (n >= 9500000) {
    ans = 754546104527913;
    cnt = 9500001;
  }
  for (int h = cnt; h <= n; h++) {
    ans += h * prime_factor(h);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// #include<bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;
// printf("%.10f\\n", n);

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll modpow(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
      res %= mod;
    }
    x = x * x % mod;
    y /= 2;
  }
  return res;
}

ll prime_factor(ll n) {
  ll gh = 1;
  for (ll i = 2; i * i <= n; i++) {
    ll ok = 1;
    while (n % i == 0) {
      ok++;
      n /= i;
    }
    gh *= ok;
  }
  if (n != 1) {
    gh *= 2;
  }
  return gh;
}

ll test[12345678], aa[12345678];
signed main() {
  ll n, m, k, q, a = -1, b = -1, c = 0, cnt = 1, ans = 0;
  string s, t;
  cin >> n;
  if (n >= 1000000) {
    ans = 7235062807366;
    cnt = 1000001;
  }
  if (n >= 2000000) {
    ans = 30326413365285;
    cnt = 2000001;
  }
  if (n >= 3000000) {
    ans = 70058785460280;
    cnt = 3000001;
  }
  if (n >= 4000000) {
    ans = 126850549017804;
    cnt = 4000001;
  }
  if (n >= 5000000) {
    ans = 200992441246692;
    cnt = 5000001;
  }
  if (n >= 6000000) {
    ans = 292711324081941;
    cnt = 6000001;
  }
  if (n >= 7000000) {
    ans = 402188826344049;
    cnt = 7000001;
  }
  if (n >= 8000000) {
    ans = 529581632274134;
    cnt = 8000001;
  }
  if (n >= 9000000) {
    ans = 675021515919670;
    cnt = 9000001;
  }
  for (int h = cnt; h <= n; h++) {
    ans += h * prime_factor(h);
  }
  cout << ans << endl;
  return 0;
}