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

//                          **********  YATIN KWATRA  **********

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rfo(i, a, b) for (int i = a; i >= b; i--)
#define vii vector<int>
#define vll vector<ll>
#define pq priority_queue
#define uom unordered_map
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<long, long>
#define mll map<ll, ll>
#define mii map<int, int>
#define vvii vector<vii>
#define vvll vector<vll>
#define mod 1000000007
#define pi 3.1415926535897932384626433832795
// #define node                    Node

using namespace std;

void INPUT() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int MAX = 10000001;

// code reference geeks for geeks

ll factor[MAX] = {0};

void generatePrimeFactors() {
  factor[1] = 1;

  for (int i = 2; i < MAX; i++)
    factor[i] = i;

  for (int i = 4; i < MAX; i += 2)
    factor[i] = 2;

  for (int i = 3; i * i < MAX; i++) {
    if (factor[i] == i) {
      for (int j = i * i; j < MAX; j += i) {
        if (factor[j] == j)
          factor[j] = i;
      }
    }
  }
}

ll calculateNoOFactors(int n) {
  if (n == 1)
    return 1LL;

  ll ans = 1;

  ll dup = factor[n];

  ll c = 1;

  ll j = n / factor[n];

  while (j != 1) {

    if (factor[j] == dup)
      c += 1;

    else {
      dup = factor[j];
      ans = ans * (c + 1);
      c = 1;
    }

    j = j / factor[j];
  }

  ans = ans * (c + 1LL);

  return ans;
}

void solve() {
  ll n;
  cin >> n;

  generatePrimeFactors();

  ll ans = 0;

  fo(i, 1, n) { ans += i * (calculateNoOFactors(i)); }

  cout << ans << endl;
}
int main() {

  FIO INPUT();

  int t;
  t = 1;
  // cin >> t;

  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

//                          **********  YATIN KWATRA  **********

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rfo(i, a, b) for (int i = a; i >= b; i--)
#define vii vector<int>
#define vll vector<ll>
#define pq priority_queue
#define uom unordered_map
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<long, long>
#define mll map<ll, ll>
#define mii map<int, int>
#define vvii vector<vii>
#define vvll vector<vll>
#define mod 1000000007
#define pi 3.1415926535897932384626433832795
// #define node                    Node

using namespace std;

void INPUT() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int N = 10000001;

ll f(ll n) {
  ll cnt = 0;
  ll ans = 1;

  while (!(n % 2)) {
    cnt++;
    n /= 2;
  }

  ans *= (cnt + 1);

  for (ll i = 3; i * i <= n; i += 2) {
    cnt = 0;

    while (!(n % i)) {
      cnt++;
      n /= i;
    }
    ans *= (1 + cnt);
  }

  if (n > 2)
    ans *= 2;

  return ans;
}

void solve() {
  ll n;
  cin >> n;

  ll ans = 0;

  fo(i, 1, n) { ans += i * (f(i)); }

  cout << ans << endl;
}
int main() {

  FIO INPUT();

  int t;
  t = 1;
  // cin >> t;

  while (t--) {
    solve();
  }
  return 0;
}