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

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\\n'
using namespace std;
typedef double ld;
typedef long long lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ld> vld;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<double> vd;
#define deb(x) cout << #x ": " << (x) << endl;

const int N = 27;
const lli INF = 1e18;
const lli MOD = 13;
const double EPS = 1e-6;

bool mm[N];
vi primes(1, 2);
void sieve() {
  for (int i = 4; i < N; i += 2)
    mm[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!mm[i])
      for (int j = i * i; j < N; j += i)
        mm[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!mm[i])
      primes.pb(i);
}

lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); }

lli lcm(lli a, lli b) {
  if (a < b)
    swap(a, b);
  lli c = gcd(a, b);
  a /= c;
  return a * b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}

lli powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

bool isPrime(lli x) {
  if (x == 1)
    return 0;
  for (auto i : primes) {
    if (i * i > x)
      return 1;
    if (x % i == 0)
      return 0;
  }
  return 1;
}

// ---- コーディングはここから！ ('-')7

int main() {
  _ lli n;
  cin >> n;
  vl v(n + 1, 0);
  fore(i, 1, n + 1) for (int j = i; j <= n; j += i) v[j]++;
  lli sum = 0;
  fore(i, 1, n + 1) sum += (v[i] * i);
  cout << sum << ENDL;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\\n'
using namespace std;
typedef double ld;
typedef long long lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;
#define deb(x) cout << #x ": " << (x) << endl;

const int N = 5e3;
const lli INF = 1e18;
const lli MOD = 13;
const double EPS = 1e-4;

bool m[N];
vi primes(1, 2);
void sieve() {
  for (int i = 4; i < N; i += 2)
    m[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!m[i])
      for (int j = i * i; j < N; j += i)
        m[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!m[i])
      primes.pb(i);
}

lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); }

lli lcm(lli a, lli b) {
  if (a < b)
    swap(a, b);
  lli c = gcd(a, b);
  a /= c;
  return a * b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}

lli powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

bool isPrime(lli x) {
  if (x == 1)
    return 0;
  for (auto i : primes) {
    if (i * i > x)
      return 1;
    if (x % i == 0)
      return 0;
  }
  return 1;
}

// ---- コーディングはここから！ ('-')7

int main() {
  _ sieve();
  int n, m;
  cin >> m;
  lli u = 1;
  lli sum = 0;
  fore(k, 1, m + 1) {
    n = k;
    u = 1;
    for (auto i : primes) {
      if (i * i > n)
        break;
      int c = 0;
      while (n % i == 0) {
        n /= i;
        c++;
      }
      u *= (c + 1);
    }
    if (n > 1)
      u *= 2;
    sum += u * k;
  }
  cout << sum << ENDL;
}