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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define long long long
#define PI 3.141592653589793238

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;
typedef tree<long int, null_type, less<long int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef cc_hash_table<
    long int, long int, hash<long int>, equal_to<long int>,
    direct_mask_range_hashing<long int>,
    hash_standard_resize_policy<hash_exponential_size_policy<>,
                                hash_load_check_resize_trigger<true>, true>>
    ht;
typedef gp_hash_table<long int, long int> HashTable;

// a(n) = Sum_{k=1..n} k/2 * floor(n/k) * floor(1 + n/k).

int main() {
  IOS;
  /*    #ifndef ONLINE_JUDGE
          freopen("in.txt",  "r",  stdin);
          freopen("out.txt", "w", stdout);
      #endif */

  long int n;
  cin >> n;

  long int ans = 0;
  for (long int i = 1; i <= n; i++)
    ans += (i * (n / i) * (1 + n / i)) / 2;

  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define long long long
#define PI 3.141592653589793238

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;
typedef tree<long int, null_type, less<long int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef cc_hash_table<
    long int, long int, hash<long int>, equal_to<long int>,
    direct_mask_range_hashing<long int>,
    hash_standard_resize_policy<hash_exponential_size_policy<>,
                                hash_load_check_resize_trigger<true>, true>>
    ht;
typedef gp_hash_table<long int, long int> HashTable;

vector<long int> primes;
void sieve(long int n) {
  vector<bool> isPrime(n + 1, true);
  isPrime[0] = isPrime[1] = false;

  for (long int i = 2; i < isPrime.size(); i++) {
    if (isPrime[i]) {
      for (long int j = i * i; j < isPrime.size(); j += i)
        isPrime[j] = false;
    }
  }

  for (long int i = 0; i < isPrime.size(); i++)
    if (isPrime[i])
      primes.push_back(i);
}

int main() {
  IOS;
  /*    #ifndef ONLINE_JUDGE
          freopen("in.txt",  "r",  stdin);
          freopen("out.txt", "w", stdout);
      #endif */

  long int m;
  cin >> m;

  sieve(m + 1);

  long int finalans = 0;

  for (long int j = 1; j <= m; j++) {
    long int ans = 1;
    long int n = j;
    for (long int i = 0; primes[i] <= n; i++) {
      if ((n % primes[i]) == 0) {
        long int cnt = 0;
        while ((n % primes[i] == 0)) {
          n /= primes[i];
          cnt++;
        }
        ans *= (cnt + 1);
      }
    }
    finalans += (j * ans);
  }

  cout << finalans;

  return 0;
}