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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int mx = 1e6 + 11;
const double PI = acos(-1);
#define MOD 1000000007

#define nl '\\n'
#define pb push_back
#define F first
#define S second
#define I insert
#define mp make_pair

#define sz(x) x.size()
#define sa(v) sort(v.begin(), v.end())
#define sd(v) sort(v.begin(), v.end(), greater<>())
#define rev(s) reverse(s.begin(), s.end())
#define tt                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define lo(i, a, b) for (ll i = a; i < b; i++)
#define be(a) (a).begin(), (a).end()
#define rbe(a) (a).rbegin(), (a).rend()

#define sqr(a) ((a) * (a))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define mem(a, b) memset(a, b, sizeof(a))

#define yes cout << "YES" << nl;
#define no cout << "NO" << nl;

#define su                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define frac                                                                   \\
  cout.unsetf(ios::floatfield);                                                \\
  cout.precision(10);                                                          \\
  cout.setf(ios::fixed, ios::floatfield);

#define dbg(args...)                                                           \\
  do {                                                                         \\
    cerr << #args << " : ";                                                    \\
    faltu(args);                                                               \\
  } while (0)
void faltu() { cerr << endl; }
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
ll f(ll x) { return x * (x - 1) / 2; }
ll div(ll n) {
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += i * f(n / i + 1);
  }
  return sum;
}

int main() {
  su;
  ll n;
  cin >> n;
  cout << div(n) << nl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int mx = 1e6 + 11;
const double PI = acos(-1);
#define MOD 1000000007

#define nl '\\n'
#define pb push_back
#define F first
#define S second
#define I insert
#define mp make_pair

#define sz(x) x.size()
#define sa(v) sort(v.begin(), v.end())
#define sd(v) sort(v.begin(), v.end(), greater<>())
#define rev(s) reverse(s.begin(), s.end())
#define tt                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define lo(i, a, b) for (ll i = a; i < b; i++)
#define be(a) (a).begin(), (a).end()
#define rbe(a) (a).rbegin(), (a).rend()

#define sqr(a) ((a) * (a))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define mem(a, b) memset(a, b, sizeof(a))

#define yes cout << "YES" << nl;
#define no cout << "NO" << nl;

#define su                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define frac                                                                   \\
  cout.unsetf(ios::floatfield);                                                \\
  cout.precision(10);                                                          \\
  cout.setf(ios::fixed, ios::floatfield);

#define dbg(args...)                                                           \\
  do {                                                                         \\
    cerr << #args << " : ";                                                    \\
    faltu(args);                                                               \\
  } while (0)
void faltu() { cerr << endl; }
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}

ll div(ll n) {
  int d = 0;
  for (int i = 1; i <= sqrt(n + 1); i++) {
    if (i * i == n)
      d++;
    else if (n % i == 0)
      d += 2;
  }
  return d;
}

int main() {
  su;
  int n;
  ll sum = 1;
  cin >> n;
  for (int i = 2; i <= n; i++) {
    sum += (i * div(i));
  }
  cout << sum << nl;
  return 0;
}