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

// #define _GLIBCXX_DEBUG
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep0(TMS) for (int CNT = 0; CNT < (int)(TMS); CNT++)
#define rep(CNT, GOAL) for (int CNT = 0; CNT < (int)(GOAL); CNT++)
#define rep2(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT < (int)(GOAL); CNT++)
#define rep3(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT > (int)(GOAL); CNT--)
#define all(CONT) begin(CONT), end(CONT)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void prec(const int &DIG) {
  cout << fixed << setprecision(DIG);
  cerr << fixed << setprecision(DIG);
}
template <typename T> void CERR(const T &ELEM) { cerr << ELEM; }
#define dbl(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << "\\n"
template <typename T, typename... Ts>
void CERR(const T &FIRST, const Ts &...REST) {
  CERR(FIRST);
  cerr << ", ";
  CERR(REST...);
}
#define dbs(...)                                                               \\
  cerr << "(" << #__VA_ARGS__ << "): (";                                       \\
  CERR(__VA_ARGS__);                                                           \\
  cerr << ")\\n"
#define itrep(ITR, CONT) for (auto ITR = begin(CONT); ITR != end(CONT); ITR++)
template <typename T> void CERR(const vector<T> &VEC) {
  cerr << "{ ";
  itrep(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
#define YN(FLG) cout << (FLG ? "YES" : "NO") << "\\n"
#define Yn(FLG) cout << (FLG ? "Yes" : "No") << "\\n"
#define yn(FLG) cout << (FLG ? "yes" : "no") << "\\n"
// const ll INF = LONG_LONG_MAX;
const int INF = INT_MAX;
// const ll MOD = 1000000007; // 998244353;

// #include <atcoder/all>
// using namespace atcoder;

// iの倍数は1~Nに[N/i]個ある->合計を求められる
// i=1~Nについて上記を加算する

int main() {
  int N;
  cin >> N;
  ll ans = 0;
  rep2(i, 1, N + 1) {
    ll m = N / i; // iの倍数の個数
    ll temp = (i + m * i) * m / 2;
    ans += temp;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using mli = map<ll, int>;
using sl = set<ll>;
using si = set<int>;
using vb = vector<bool>;
#define rep0(TMS) for (int CNT = 0; CNT < (int)(TMS); CNT++)
#define rep(CNT, GOAL) for (int CNT = 0; CNT < (int)(GOAL); CNT++)
#define rep2(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT < (int)(GOAL); CNT++)
#define rep3(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT > (int)(GOAL); CNT--)
#define all(CONT) begin(CONT), end(CONT)
#define itrep(ITR, CONT) for (auto ITR = begin(CONT); ITR != end(CONT); ITR++)
#define itrep1(ITR, CONT)                                                      \\
  for (auto ITR = next(begin(CONT)); ITR != end(CONT); ITR++)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void prec(const int &DIG) {
  cerr << fixed << setprecision(DIG);
  cout << fixed << setprecision(DIG);
}
template <typename T> void CERR(const T &ELEM) { cerr << ELEM; }
template <typename T, typename... Ts>
void CERR(const T &FIRST, const Ts &...REST) {
  CERR(FIRST);
  cerr << ", ";
  CERR(REST...);
}
template <typename T1, typename T2> void CERR(const pair<T1, T2> &PAIR) {
  cerr << "(";
  CERR(PAIR.first);
  cerr << ", ";
  CERR(PAIR.second);
  cerr << ")";
}
template <typename T> void CERR(const vector<T> &VEC) {
  cerr << "{ ";
  itrep(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR1(const vector<T> &VEC) {
  cerr << "{ ";
  itrep1(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR(const set<T> &SET) {
  cerr << "{ ";
  itrep(ITR, SET) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR(const multiset<T> &MULTISET) {
  cerr << "{ ";
  itrep(ITR, MULTISET) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T1, typename T2> void CERR(const map<T1, T2> &MAP) {
  cerr << "{ ";
  itrep(ITR, MAP) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
#define db(OBJ)                                                                \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << ", "
#define dbl(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << "\\n"
#define db1(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR1(OBJ);                                                                  \\
  cerr << "\\n"
#define dbs(...)                                                               \\
  cerr << "(" << #__VA_ARGS__ << "): (";                                       \\
  CERR(__VA_ARGS__);                                                           \\
  cerr << ")\\n"
#define dbvv(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep(CNT, VV.size()) {                                                        \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR(VV[CNT]);                                                             \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db01(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep(CNT, VV.size()) {                                                        \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR1(VV[CNT]);                                                            \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db10(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep2(CNT, 1, VV.size()) {                                                    \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR(VV[CNT]);                                                             \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db11(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep2(CNT, 1, VV.size()) {                                                    \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR1(VV[CNT]);                                                            \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define YN(FLG) cout << (FLG ? "YES" : "NO") << "\\n"
#define Yn(FLG) cout << (FLG ? "Yes" : "No") << "\\n"
#define yn(FLG) cout << (FLG ? "yes" : "no") << "\\n"
// const ll INF = 1'000'000'000'000'000'007;
const int INF = 1'000'000'007;
const ll MOD = 1'000'000'007; // 998'244'353;

pli divs(ll N, ll prm) {
  assert(N > 0);
  int deg = 0;
  for (; N % prm == 0; deg++)
    N /= prm;
  return pli(N, deg);
}
bool isprime(ll N) {
  assert(N > 0);
  if (N == 1)
    return false;
  for (int k = 2; k * k <= N; k++)
    if (N % k == 0)
      return false;
  return true;
}
mli pf(ll N) {
  assert(N > 0);
  mli res;
  for (ll prm = 2; prm * prm <= N; prm++)
    if (N % prm == 0)
      tie(N, res[prm]) = divs(N, prm);
  if (N > 1)
    res[N] = 1;
  return res;
}
int NPF(ll N) {
  assert(N > 0);
  int res = 0;
  for (pli p : pf(N))
    res += p.second;
  return res;
}

ll npow(ll x, int n) {
  assert(n >= 0);
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}
int ND(ll N) {
  assert(N > 0);
  int res = 1;
  for (pli p : pf(N))
    res *= (p.second + 1);
  return res;
}
ll SD(ll N) {
  assert(N > 0);
  ll res = 1;
  for (pli p : pf(N))
    res *= (npow(p.first, p.second + 1) - 1) / (p.first - 1);
  return res;
}
sl divisors(ll N) {
  assert(N > 0);
  sl res;
  queue<ll> q;
  q.push(1LL);
  res.insert(1LL);
  for (pll p : pf(N)) {
    ll prm, deg;
    tie(prm, deg) = p;
    queue<ll> temp;
    while (!q.empty()) {
      ll x = q.front();
      q.pop();
      temp.push(x);
      rep0(deg) {
        x *= prm;
        temp.push(x);
        res.insert(x);
      }
    }
    q = temp;
  }
  return res;
}
si SE(int N) {
  assert(N > 0);
  si res;
  vb v(N + 1, true);
  v[0] = v[1] = false;
  int prm = 2;
  while (prm <= N) {
    res.insert(prm);
    for (int k = 2; k * prm <= N; k++)
      v[k * prm] = false;
    prm++;
    while (prm <= N and !v[prm])
      prm++;
  }
  return res;
}

const int MAX = 1000000;
vector<bool> s(MAX + 1, true);
vi p;
vector<ll> f(MAX + 1, 1);

void init() {
  s[0] = s[1] = false;
  rep(p, MAX + 1) {
    while (p <= MAX and !s[p])
      p++;
    for (int k = 2; k * p <= MAX; k++) {
      s[k * p] = false;
    }
  }
  rep(p, MAX + 1) {
    if (!s[p])
      continue;
    vector<ll> temp(MAX + 1, 1);
    for (ll k = p; k <= MAX; k *= p) {
      for (ll m = 1; m * k <= MAX; m++) {
        temp[m * k]++;
      }
    }
    rep(i, MAX) f[i] *= temp[i];
    // dbl(p); dbl(temp);
  }
  // dbl(f);
}

vector<ll> memo(MAX + 1, -1);

ll solve(int x) {
  ll res = 1;
  for (int pr : p) {
    if (x % pr == 0) {
      ll deg = 0, k = 1;
      while (x % pr == 0) {
        deg++;
        x /= pr;
        k *= pr;
      }
      res *= k * (deg + 1);
    }
  }
  return res;
}

int main() {
  init();
  // dbvv(s); dbl(p);
  int N;
  cin >> N;
  ll ans = 0;
  rep2(i, 1, N + 1) {
    dbs(i, f[i]);
    ans += f[i];
  }
  cout << ans << endl;
}