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

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define bfr(i, a, b) for (lli i = a; i >= b; i--)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL), cout.tie(NULL);
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define mod 1000000007LL
#define mod2 998244353LL
#define all(vec) vec.begin(), vec.end()
#define ld long double
#define pb push_back
#define ordered_set                                                            \\
  tree<lli, null_type, less<lli>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

lli power(lli x, lli y, lli m) {
  lli res = 1;
  while (y > 0) {
    if (y & 1)
      res = ((res % m) * (x % m)) % m;
    y >>= 2;
    x = ((x % m) * (x % m)) % m;
  }
  return res % m;
}

int main() {

  fio

      lli n;
  cin >> n;

  lli s[n + 100] = {0};

  fr(i, 2, n + 100) {
    if (s[i] == 0)
      for (lli j = i + i; j < n + 100; j += i) {
        if (s[j] == 0)
          s[j] = 1;
        lli cnt = 0, l = j;
        while (l % i == 0) {
          l /= i;
          cnt++;
        }
        s[j] *= (cnt + 1);
      }
  }

  s[1] = 1;
  lli ans = 0;
  fr(i, 1, n + 1) if (s[i] == 0) ans += (i * 2);
  else ans += (i * s[i]);

  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define bfr(i, a, b) for (lli i = a; i >= b; i--)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL), cout.tie(NULL);
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define mod 1000000007LL
#define mod2 998244353LL
#define all(vec) vec.begin(), vec.end()
#define ld long double
#define pb push_back
#define ordered_set                                                            \\
  tree<lli, null_type, less<lli>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

lli power(lli x, lli y, lli m) {
  lli res = 1;
  while (y > 0) {
    if (y & 1)
      res = ((res % m) * (x % m)) % m;
    y >>= 2;
    x = ((x % m) * (x % m)) % m;
  }
  return res % m;
}

int main() {

  fio

      lli n;
  cin >> n;
  lli ans = 1;

  fr(i, 2, n + 1) {
    lli cnt = 0, k = 1, l = i;
    for (int j = 2; j <= ceil(sqrt(i * 1.0)); j++)
      if (l % j == 0) {
        cnt = 0;
        while (l % j == 0) {
          l /= j, cnt++;
        }
        k *= (cnt + 1);
      } else if (l == 1)
        break;

    if (l != 1)
      k *= 2;

    ans += i * k;
  }
  cout << ans;

  return 0;
}