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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define endl '\\n'
#define ff first
#define ss second
#define rev(a) a.rbegin(), a.rend()
#define all(a) a.begin(), a.end()
#define pi pair<int, int>
#define vii vector<pi>
#define size(a) (int)(a.size())
#define fill(s, x, n)                                                          \\
  vector
 x(n);                                                              \\
  rep(i, 0, n) cin >> x[i];
#define debug(...)                                                             \\
  cerr << " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] ";
#define debugv(...)                                                            \\
  for (int i = 0; i < size(__VA_ARGS__); i++)                                  \\
    cerr << " [" << #__VA_ARGS__ << i << ": " << (__VA_ARGS__)[i] << "] ";
const int mod = 1e8;
const int inf = 1e9 + 5;
const int inf64 = 1e18 + 5;
//           -------"Where is your Pen and Paper?"-------
int prime[10000001];
void sieve() {
  prime[1] = 1;
  for (int i = 2; i < 10000001; i++) {
    if (prime[i] == 0) {
      prime[i] = i;
      for (int j = 2 * i; j < 10000001; j += i) {
        if (prime[j] == 0)
          prime[j] = i;
      }
    }
  }
}
void solve() {
  int n;
  cin >> n;
  sieve();
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = i;
    int t = 1;
    int cnt = 0;
    int prev = prime[x];
    while (x > 1) {
      if (prime[x] != prev) {
        prev = prime[x];
        t *= (cnt + 1);
        cnt = 1;
      } else {
        cnt++;
      }
      x /= prime[x];
    }
    t *= (cnt + 1);
    // debug(t);
    ans += i * t;
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /*
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  */
  int test;
  // cin>>test;
  test = 1;
  // int cse=1;
  while (test--) {
    // cout<<"Case #"<<cse<<": ";
    solve();
    cout << endl;
    // cse++;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define endl '\\n'
#define ff first
#define ss second
#define rev(a) a.rbegin(), a.rend()
#define all(a) a.begin(), a.end()
#define pi pair<int, int>
#define vii vector<pi>
#define size(a) (int)(a.size())
#define fill(s, x, n)                                                          \\
  vector
 x(n);                                                              \\
  rep(i, 0, n) cin >> x[i];
#define debug(...)                                                             \\
  cerr << " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] ";
#define debugv(...)                                                            \\
  for (int i = 0; i < size(__VA_ARGS__); i++)                                  \\
    cerr << " [" << #__VA_ARGS__ << i << ": " << (__VA_ARGS__)[i] << "] ";
const int mod = 1e8;
const int inf = 1e9 + 5;
const int inf64 = 1e18 + 5;
//           -------"Where is your Pen and Paper?"-------
int prime[10000001];
void sieve() {
  prime[1] = 1;
  for (int i = 2; i < 10000001; i++) {
    if (prime[i] == 0) {
      prime[i] = i;
      for (int j = 2 * i; j < 10000001; j += i) {
        if (prime[j] == 0)
          prime[j] = i;
      }
    }
  }
}
void solve() {
  int n;
  cin >> n;
  sieve();
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = i;
    unordered_map<int, int> mp;
    while (x > 1) {
      mp[prime[x]]++;
      x /= prime[x];
    }
    int t = 1;
    for (auto pp : mp) {
      t *= (pp.ss + 1);
    }
    ans += i * t;
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /*
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  */
  int test;
  // cin>>test;
  test = 1;
  // int cse=1;
  while (test--) {
    // cout<<"Case #"<<cse<<": ";
    solve();
    cout << endl;
    // cse++;
  }
  return 0;
}