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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE> void print_vec(const vector<TYPE> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}

template <typename TYPE> void print_vec2(const vector<vector<TYPE>> &v) {
  cout << endl;
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   " << endl;
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

int main() {

  ll N;
  cin >> N;

  vector<ll> num_divisors(N + 1, 0); // num_divisors[i]: iの約数の個数
  for (ll i = 1; i <= N; i++) {
    for (ll j = 1; i * j <= N; j++) {
      num_divisors[i * j]++;
    }
  }

  ll ans = 0;
  for (ll i = 1; i <= N; i++) {
    ans += i * num_divisors[i];
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE> void print_vec(const vector<TYPE> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}

template <typename TYPE> void print_vec2(const vector<vector<TYPE>> &v) {
  cout << endl;
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   " << endl;
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

vector<pair<long long, int>> factorize(long long n) {
  vector<pair<long long, int>> res;

  if (n % 2 == 0) {
    res.emplace_back(2, 0);
    while (n % 2 == 0) {
      n /= 2;
      res.back().second++;
    }
  }

  for (int i = 3; i * i <= n; i += 2) {
    if (n % i)
      continue;
    res.emplace_back(i, 0);
    while (n % i == 0) {
      n /= i;
      res.back().second++;
    }
  }
  if (n != 1)
    res.emplace_back(n, 1);
  return res;
}

long long num_divisors(long long n) {

  vector<pair<long long, int>> fact = factorize(n);

  long long res = 1;
  for (int i = 0; i < fact.size(); i++) {
    res *= fact[i].second + 1;
  }

  return res;
}

int main() { // https://atcoder.jp/contests/abc106/submissions/9915055
  ll N;
  cin >> N;

  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (N == 2) {
    cout << 5 << endl;
    return 0;
  }
  ll ans = 5; // K=1 は 1,   K=2は4 なので 5

  typedef pair<ll, int> P;
  for (ll k = 3; k <= N; k++) {

    vector<P> vec; // first:素数  second　個数
    ll tmp = k;
    ll cnt = 1;
    for (ll i = 2; i * i <= tmp; i++) {
      int num = 0;
      if (tmp % i != 0)
        continue;
      while (tmp % i == 0) {
        num++;
        tmp /= i;
      }

      // vec.push_back(P(i, num));
      // vec.emplace_back(i, num);
      cnt *= num + 1;
    }
    // if(tmp != 1) vec.push_back(P(tmp, 1));
    if (tmp != 1) {
      // vec.emplace_back(tmp, 1);
      cnt *= 1 + 1;
    }
    // ll cnt = 1;
    // for(int i=0; i<vec.size(); i++){
    //   cnt *= vec[i].second+1;
    // }

    // cout << " k: " << k << " cnt: " << cnt << endl;
    ans += k * cnt;
  }

  cout << ans << endl;
}