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

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

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

// 約数列挙
template <class T> vector<T> dividers(T n) {
  vector<T> ret;
  for (T x = 1; x * x <= n; x++) {
    if (n % x == 0) {
      ret.push_back(x);
      if (x * x != n)
        ret.push_back(n / x);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

int main() {
  lint N;
  cin >> N;

  vector<lint> divs(N + 1, 0);
  for (int x = 1; x <= N; x++) {
    for (int y = 1; y <= (int)N / x; y++) {
      divs[x * y]++;
    }
  }

  lint ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += divs[i] * i;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \\
  cerr << #v ":[ ";                                                            \\
  for (auto macro_vi : v) {                                                    \\
    cerr << macro_vi << " ";                                                   \\
  }                                                                            \\
  cerr << "]" << endl;
#define ddump(v)                                                               \\
  cerr << #v ":" << endl;                                                      \\
  for (auto macro_row : v) {                                                   \\
    cerr << "[";                                                               \\
    for (auto macro__vi : macro_row) {                                         \\
      cerr << macro__vi << " ";                                                \\
    }                                                                          \\
    cerr << "]" << endl;                                                       \\
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

// 約数列挙
template <class T> vector<T> dividers(T n) {
  vector<T> ret;
  for (T x = 1; x * x <= n; x++) {
    if (n % x == 0) {
      ret.push_back(x);
      if (x * x != n)
        ret.push_back(n / x);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

int main() {
  lint N;
  cin >> N;

  vector<lint> divs(N + 1, 0);
  for (int x = 1; x <= N; x++) {
    divs[x] = dividers(x).size();
  }

  lint ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += divs[i] * i;
  }
  cout << ans << endl;
  return 0;
}