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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,
    ans, tmp, hoge, fuga, piyo, sum;
ll A[200100], B[200100], C[200100], D[200100];
string st, str;
char ch;

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0;
    while (N % a == 0) {
      ++ex;
      N /= a;
    }
    res.push_back({a, ex});
  }
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main() {
  cin >> n;
  if (n <= 100000) {
    m = 1;
  } else if (n <= 200000) {
    ans += 60838190672;
    m = 100001;
  } else if (n <= 300000) {
    ans += 257212470774;
    m = 200001;
  } else if (n <= 400000) {
    ans += 596981215450;
    m = 300001;
  } else if (n <= 500000) {
    ans += 1084296609104;
    m = 400001;
  } else if (n <= 600000) {
    ans += 1722125036282;
    m = 500001;
  } else if (n <= 700000) {
    ans += 2512661322551;
    m = 600001;
  } else if (n <= 800000) {
    ans += 3457770622390;
    m = 700001;
  } else if (n <= 900000) {
    ans += 4558987534475;
    m = 800001;
  } else if (n <= 1000000) {
    ans += 5817729717828;
    m = 900001;
  } else if (n <= 1100000) {
    ans += 7235062807366;
    m = 1000001;
  } else if (n <= 1200000) {
    ans += 8812039622862;
    m = 1100001;
  } else if (n <= 1300000) {
    ans += 10549659220862;
    m = 1200001;
  } else if (n <= 1400000) {
    ans += 12448845177256;
    m = 1300001;
  } else if (n <= 1500000) {
    ans += 14510332007222;
    m = 1400001;
  } else if (n <= 1600000) {
    ans += 16734924755096;
    m = 1500001;
  } else if (n <= 1700000) {
    ans += 19123138141378;
    m = 1600001;
  } else if (n <= 1800000) {
    ans += 21675833225770;
    m = 1700001;
  } else if (n <= 1900000) {
    ans += 24393634293809;
    m = 1800001;
  } else if (n <= 2000000) {
    ans += 27276821356759;
    m = 1900001;
  } else if (n <= 2100000) {
    ans += 30326413365285;
    m = 2000001;
  } else if (n <= 2200000) {
    ans += 33542358268005;
    m = 2100001;
  } else if (n <= 2300000) {
    ans += 36925459066686;
    m = 2200001;
  } else if (n <= 2400000) {
    ans += 40476248899052;
    m = 2300001;
  } else if (n <= 2500000) {
    ans += 44194833598353;
    m = 2400001;
  } else if (n <= 2600000) {
    ans += 48081994935671;
    m = 2500001;
  } else if (n <= 2700000) {
    ans += 52138123487538;
    m = 2600001;
  } else if (n <= 2800000) {
    ans += 56363615930452;
    m = 2700001;
  } else if (n <= 2900000) {
    ans += 60758384185743;
    m = 2800001;
  } else if (n <= 3000000) {
    ans += 65323182791149;
    m = 2900001;
  } else if (n <= 3100000) {
    ans += 70058785460280;
    m = 3000001;
  } else if (n <= 3200000) {
    ans += 74964555109988;
    m = 3100001;
  } else if (n <= 3300000) {
    ans += 80041527790312;
    m = 3200001;
  } else if (n <= 3400000) {
    ans += 85290161753688;
    m = 3300001;
  } else if (n <= 3500000) {
    ans += 90709780843282;
    m = 3400001;
  } else if (n <= 3600000) {
    ans += 96301520878991;
    m = 3500001;
  } else if (n <= 3700000) {
    ans += 102065855149715;
    m = 3600001;
  } else if (n <= 3800000) {
    ans += 108002269398516;
    m = 3700001;
  } else if (n <= 3900000) {
    ans += 114111807921375;
    m = 3800001;
  } else if (n <= 4000000) {
    ans += 120394329084663;
    m = 3900001;
  } else if (n <= 4100000) {
    ans += 126850549017804;
    m = 4000001;
  } else if (n <= 4200000) {
    ans += 133479467089246;
    m = 4100001;
  } else if (n <= 4300000) {
    ans += 140282617787827;
    m = 4200001;
  } else if (n <= 4400000) {
    ans += 147259491582477;
    m = 4300001;
  } else if (n <= 4500000) {
    ans += 154411235469265;
    m = 4400001;
  } else if (n <= 4600000) {
    ans += 161737467121483;
    m = 4500001;
  } else if (n <= 4700000) {
    ans += 169238102781982;
    m = 4600001;
  } else if (n <= 4800000) {
    ans += 176913242475822;
    m = 4700001;
  } else if (n <= 4900000) {
    ans += 184764302482181;
    m = 4800001;
  } else if (n <= 5000000) {
    ans += 192790925674795;
    m = 4900001;
  } else if (n <= 5100000) {
    ans += 200992441246692;
    m = 5000001;
  } else if (n <= 5200000) {
    ans += 209370037656611;
    m = 5100001;
  } else if (n <= 5300000) {
    ans += 217923890158764;
    m = 5200001;
  } else if (n <= 5400000) {
    ans += 226653316365413;
    m = 5300001;
  } else if (n <= 5500000) {
    ans += 235560022983960;
    m = 5400001;
  } else if (n <= 5600000) {
    ans += 244642548171917;
    m = 5500001;
  } else if (n <= 5700000) {
    ans += 253902107151677;
    m = 5600001;
  } else if (n <= 5800000) {
    ans += 263338334303632;
    m = 5700001;
  } else if (n <= 5900000) {
    ans += 272951354222680;
    m = 5800001;
  } else if (n <= 6000000) {
    ans += 282742772106696;
    m = 5900001;
  } else if (n <= 6100000) {
    ans += 292711324081941;
    m = 6000001;
  } else if (n <= 6200000) {
    ans += 302856699362737;
    m = 6100001;
  } else if (n <= 6300000) {
    ans += 313180253381239;
    m = 6200001;
  } else if (n <= 6400000) {
    ans += 323682486495499;
    m = 6300001;
  } else if (n <= 6500000) {
    ans += 334361760892115;
    m = 6400001;
  } else if (n <= 6600000) {
    ans += 345219904513529;
    m = 6500001;
  } else if (n <= 6700000) {
    ans += 356256480873339;
    m = 6600001;
  } else if (n <= 6800000) {
    ans += 367470747430202;
    m = 6700001;
  } else if (n <= 6900000) {
    ans += 378864539458650;
    m = 6800001;
  } else if (n <= 7000000) {
    ans += 390437889429971;
    m = 6900001;
  } else if (n <= 7100000) {
    ans += 402188826344049;
    m = 7000001;
  } else if (n <= 7200000) {
    ans += 414118956306590;
    m = 7100001;
  } else if (n <= 7300000) {
    ans += 426229374669116;
    m = 7200001;
  } else if (n <= 7400000) {
    ans += 438519040875971;
    m = 7300001;
  } else if (n <= 7500000) {
    ans += 450987322450746;
    m = 7400001;
  } else if (n <= 7600000) {
    ans += 463635895079489;
    m = 7500001;
  } else if (n <= 7700000) {
    ans += 476465015795706;
    m = 7600001;
  } else if (n <= 7800000) {
    ans += 489473701762655;
    m = 7700001;
  } else if (n <= 7900000) {
    ans += 502662434241804;
    m = 7800001;
  } else if (n <= 8000000) {
    ans += 516031076204871;
    m = 7900001;
  } else if (n <= 8100000) {
    ans += 529581632274134;
    m = 8000001;
  } else if (n <= 8200000) {
    ans += 543311223215741;
    m = 8100001;
  } else if (n <= 8300000) {
    ans += 557220889209512;
    m = 8200001;
  } else if (n <= 8400000) {
    ans += 571311613610574;
    m = 8300001;
  } else if (n <= 8500000) {
    ans += 585584064718129;
    m = 8400001;
  } else if (n <= 8600000) {
    ans += 600036645526326;
    m = 8500001;
  } else if (n <= 8700000) {
    ans += 614670553358276;
    m = 8600001;
  } else if (n <= 8800000) {
    ans += 629486032479553;
    m = 8700001;
  } else if (n <= 8900000) {
    ans += 644482906053419;
    m = 8800001;
  } else if (n <= 9000000) {
    ans += 659661281999214;
    m = 8900001;
  } else if (n <= 9100000) {
    ans += 675021515919670;
    m = 9000001;
  } else if (n <= 9200000) {
    ans += 690561394962114;
    m = 9100001;
  } else if (n <= 9300000) {
    ans += 706285619557381;
    m = 9200001;
  } else if (n <= 9400000) {
    ans += 722190402701363;
    m = 9300001;
  } else if (n <= 9500000) {
    ans += 738276491989247;
    m = 9400001;
  } else if (n <= 9600000) {
    ans += 754546104527913;
    m = 9500001;
  } else if (n <= 9700000) {
    ans += 770997444637057;
    m = 9600001;
  } else if (n <= 9800000) {
    ans += 787630213195247;
    m = 9700001;
  } else if (n <= 9900000) {
    ans += 804447590347769;
    m = 9800001;
  } else {
    ans += 821446543739323;
    m = 9900001;
  }
  for (int i = m; i <= n; i++) {
    const auto &pf = prime_factorize(i);
    long long res = 1;
    for (auto p : pf)
      res *= p.second + 1;
    ans += i * res;
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,
    ans, tmp, hoge, fuga, piyo, sum;
ll A[200100], B[200100], C[200100], D[200100];
string st, str;
char ch;

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0;
    while (N % a == 0) {
      ++ex;
      N /= a;
    }
    res.push_back({a, ex});
  }
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

int main() {
  cin >> n;
  long double nn = (long double)n;
  for (int i = 1; i <= n; i++) {
    const auto &pf = prime_factorize(i);
    long long res = 1;
    for (auto p : pf)
      res *= p.second + 1;
    ans += i * res;
  }
  cout << ans;
  return 0;
}