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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define MAXN 100001
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> pipii;
typedef priority_queue<pii, vector<pii>, greater<pii>> minpq;
typedef priority_queue<pii> maxpq;
// const double PI = acos(-1);
const int mod = 1000000007;
int cnt = 0;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int m = n / i;
    ans += i * m * (m + 1) / 2;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define MAXN 100001
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> pipii;
typedef priority_queue<pii, vector<pii>, greater<pii>> minpq;
typedef priority_queue<pii> maxpq;
// const double PI = acos(-1);
const int mod = 1000000007;
int cnt = 0;
void sieveOfEratosthenes(int N, int s[]) {
  // Create a boolean array "prime[0..n]" and
  // initialize all entries in it as false.
  vector<bool> prime(N + 1, false);

  // Initializing smallest factor equal to 2
  // for all the even numbers
  for (int i = 2; i <= N; i += 2)
    s[i] = 2;

  // For odd numbers less then equal to n
  for (int i = 3; i <= N; i += 2) {
    if (prime[i] == false) {
      // s(i) for a prime is the number itself
      s[i] = i;

      // For all multiples of current prime number
      for (int j = i; j * i <= N; j += 2) {
        if (prime[i * j] == false) {
          prime[i * j] = true;

          // i is the smallest prime factor for
          // number "i*j".
          s[i * j] = i;
        }
      }
    }
  }
}

// Function to generate prime factors and its power
int generatePrimeFactors(int N) {
  // s[i] is going to store smallest prime factor
  // of i.
  int s[N + 1];
  int ans = 1;

  // Filling values in s[] using sieve
  sieveOfEratosthenes(N, s);

  // printf("Factor Power\\n");

  int curr = s[N]; // Current prime factor of N
  int cnt = 1;     // Power of current prime factor

  // Printing prime factors and their powers
  while (N > 1) {
    N /= s[N];

    // N is now N/s[N].  If new N als has smallest
    // prime factor as curr, increment power
    if (curr == s[N]) {
      cnt++;
      continue;
    }
    ans *= (cnt + 1);
    // printf("%d\\t%d\\n", curr, cnt);

    // Update current prime factor as s[N] and
    // initializing count as 1.
    curr = s[N];
    cnt = 1;
  }
  return ans;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    // cout<<i*(getFactorization(i).size()+1)<<" ";
    ans += i * generatePrimeFactors(i);
  }
  cout << ans << endl;
}