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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define faster                                                                 \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define ll long long
#define ld long double
#define endl "\\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPE(i, a, b) for (ll i = a; i <= b; i++)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define bins(v, x) binary_search(v.begin(), v.end(), x)
const double pi = 2 * acos(0.0);
const int INF = 1e9 + 1;
const int mod = 1e9 + 7;
const int mod1 = 998244353;

ll modExp(ll base, ll exp, ll mod) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1) {
      res = (res * base) % mod;
    }
    exp = exp >> 1;
    base = (base * base) % mod;
  }
  return res;
}

ll inverse(ll base, ll exp, ll mod) { return modExp(base, exp - 2, mod); }

bool compare(const pair<int, int> &i, const pair<int, int> &j) {
  return (i.S > j.S || (i.S == j.S && i.F > j.F));
}

void test() {
  ll n;
  cin >> n;

  ll fac[n + 1];
  for (int i = 1; i <= n; i++) {
    fac[i] = 1;
  }

  for (int i = 2; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      fac[j]++;
    }
  }

  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += i * fac[i];
  }
  cout << sum;
}

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  faster;
  cout.precision(10);
  cout << fixed;

  int t = 1;
  // cin >> t;
  while (t--) {
    test();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define faster                                                                 \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define ll long long
#define ld long double
#define endl "\\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPE(i, a, b) for (ll i = a; i <= b; i++)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define bins(v, x) binary_search(v.begin(), v.end(), x)
const double pi = 2 * acos(0.0);
const int INF = 1e9 + 1;
const int mod = 1e9 + 7;
const int mod1 = 998244353;

ll modExp(ll base, ll exp, ll mod) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1) {
      res = (res * base) % mod;
    }
    exp = exp >> 1;
    base = (base * base) % mod;
  }
  return res;
}

ll inverse(ll base, ll exp, ll mod) { return modExp(base, exp - 2, mod); }

bool compare(const pair<int, int> &i, const pair<int, int> &j) {
  return (i.S > j.S || (i.S == j.S && i.F > j.F));
}

const int mxN = 1e7 + 1;
vector<ll> fac(mxN, 0);

void test() {
  ll n;
  cin >> n;

  unordered_set<int> s;
  for (int i = 1; i <= n; i++) {
    s.clear();
    for (int j = 1; j <= sqrt(i); j++) {
      if (i % j == 0) {
        s.insert(j);
        s.insert(i / j);
      }
    }
    fac[i] = s.size();
  }

  vector<ll> dp(mxN);
  dp[0] = 0, dp[1] = fac[1];
  for (int i = 2; i <= n; i++) {
    dp[i] = dp[i - 1] + i * fac[i];
  }

  cout << dp[n];
}

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  faster;
  cout.precision(10);
  cout << fixed;

  int t = 1;
  // cin >> t;
  while (t--) {
    test();
  }
  return 0;
}