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

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

#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
template <class T> void dump(T &&t) { cerr << t << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}

std::vector<ll> divisor(ll n) {
  std::vector<ll> div;
  for (ll p = 1; p * p <= n; ++p) {
    if (n % p == 0) {
      div.push_back(p);
      if (p * p != n)
        div.push_back(n / p);
    }
  }
  std::sort(div.begin(), div.end());
  return div;
}

signed main() {

  ll n;
  cin >> n;
  vector<ll> ans(n + 1);
  vector<int> prime(n + 1, 1);

  for (ll i = 1; i <= n; ++i) {
    // if(prime[i])ans[i]++;
    ans[i]++;
    for (ll j = i + i; j <= n; j += i) {
      ans[j]++;
    }
  }

  ll res = 0;
  rep(i, n + 1) res += (i * ans[i]);
  cout << (res) << endl;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
template <class T> void dump(T &&t) { cerr << t << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}

std::vector<ll> divisor(ll n) {
  std::vector<ll> div;
  for (ll p = 1; p * p <= n; ++p) {
    if (n % p == 0) {
      div.push_back(p);
      if (p * p != n)
        div.push_back(n / p);
    }
  }
  std::sort(div.begin(), div.end());
  return div;
}

signed main() {

  ll n;
  cin >> n;
  vector<ll> ans(n + 1);
  vector<int> prime(n + 1, 1);
  for (int i = 2; i <= n; ++i) {
    if (prime[i])
      for (int j = i + i; j <= n; j += i)
        prime[j] = 0;
  }
  for (ll i = 1; i <= n; ++i) {
    // if(prime[i])ans[i]++;
    ans[i]++;
    for (ll j = i + i; j <= n; j += i) {
      ans[j]++;
    }
  }
  // dump(ans);
  rep(i, n + 1) {
    if (divisor(i).size() != ans[i])
      dump(i, ans[i], divisor(i).size());
  }
  ll res = 0;
  rep(i, n + 1) res += (i * ans[i]);
  cout << (res) << endl;
}