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

#include <bits/stdc++.h>
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define per(i, a, b) for (auto i = (a); i >= (b); i--)
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define int_inf 0x3f3f3f3f
#define int_max 2147483647
#define ll_max 9223372036854775807
#define ull_max 18446744073709551615
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lowbit(x) (x) & (-x)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eps 1e-8
#define all(v) (v.begin(), v.end())
#define db double
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
const double inf = 1e100;
template <class T> inline T read() {
  T x = 0, f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    s = getchar();
  };
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + s - 48;
    s = getchar();
  };
  return x * f;
}
template <typename T> inline void wri(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  };
  if (x / 10)
    wri(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char s) {
  wri(x);
  putchar(s);
}
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
// 关闭流同步就不能手写读入优化啦
int vis[10001000];
ll p[10001000];
ll ans[10001000];
void init() {
  for (int i = 1; i <= 10000000; i++)
    p[i] = 1;
  for (int i = 2; i <= 10000000; i++) {
    if (vis[i])
      continue;
    p[i] = 2;
    for (int j = 2 * i; j <= 10000000; j += i) {
      ll r = 1;
      int k = j;
      int cnt = 0;
      while (k % i == 0) {
        cnt++;
        k /= i;
      }
      p[j] *= (cnt + 1);
      vis[j] = 1;
    }
  }
  for (int i = 1; i <= 10000000; i++) {
    p[i] = p[i - 1] + 1ll * p[i] * i;
    // cout<<i<<" "<<ans[i]<<endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  init();
  cout << p[n] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define per(i, a, b) for (auto i = (a); i >= (b); i--)
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define int_inf 0x3f3f3f3f
#define int_max 2147483647
#define ll_max 9223372036854775807
#define ull_max 18446744073709551615
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lowbit(x) (x) & (-x)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eps 1e-8
#define all(v) (v.begin(), v.end())
#define db double
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
const double inf = 1e100;
template <class T> inline T read() {
  T x = 0, f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    s = getchar();
  };
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + s - 48;
    s = getchar();
  };
  return x * f;
}
template <typename T> inline void wri(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  };
  if (x / 10)
    wri(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char s) {
  wri(x);
  putchar(s);
}
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
// 关闭流同步就不能手写读入优化啦
int vis[10001000];
vector<pii> p[10001000];
ll ans[10001000];
void init() {
  for (int i = 2; i <= 10000000; i++) {
    if (vis[i])
      continue;
    p[i].pb({i, 1});
    for (int j = 2 * i; j <= 10000000; j += i) {
      int k = j;
      int cnt = 0;
      while (k % i == 0) {
        cnt++;
        k /= i;
      }
      p[j].pb({i, cnt});
      vis[j] = 1;
    }
  }
  for (int i = 1; i <= 10000000; i++) {
    ll r = 1;
    for (auto u : p[i]) {
      r *= (1ll * u.se + 1ll);
    }
    ans[i] = ans[i - 1] + 1ll * r * i;
    // cout<<i<<" "<<ans[i]<<endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  init();
  cout << ans[n] << endl;
  return 0;
}