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

#include <iostream>
using namespace std;

const long long MAX = 10000001;

long long factor[MAX] = {0};

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

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

long long calculateNoOFactors(long long n) {
  if (n == 1)
    return 1;

  long long ans = 1;

  long long dup = factor[n];
  long long c = 1;
  long long 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 + 1);

  return ans;
}

int main() {
  generatePrimeFactors();
  long long n, ans = 0;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    long long x = calculateNoOFactors(i);
    // cout << x << " ";
    ans += i * x;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:


// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")

//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)
#pragma GCC optimize("unroll-loops")
// -------------------</optimizations>--------------------
#include <algorithm> // sort()
#include <bitset>
#include <climits> // for INT_MAX & INT_MIN
#include <cmath>   // for pow(), sqrt(), log(), log10(), ....
#include <cstring> // for memset()
#include <iomanip> // for setprecision()
#include <iostream>
#include <list> // for list
#include <map>  // for ordered map
#include <set>  // for ordered set
#include <unordered_map>
#include <unordered_set> // for unordered_set
#include <vector>        // for vectors

#define C                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
typedef long long ll;
#define INF INT_MAX
#define NINF INT_MIN
#define MOD 1000000007
const double pi = acos(-1); // value of pi

bool isVowel(char ch) {
  if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
    return 1;
  }
  return 0;
}

bool compare(pair<ll, ll> a, pair<ll, ll> b) { return a.second < b.second; }

bool isPrime(ll n) {
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

map<ll, list<ll>> m;
// map<ll, ll> m;

int main() {
  C
      //     freopen("input.txt","r",stdin);
      // 	freopen("output.txt","w",stdout);
      int t = 1;
  // cin >> t;
  while (t--) {
    ll n, ans = 0;
    cin >> n;
    for (ll j = 1; j <= n; j++) {
      ll x = 0;
      for (ll i = 1; i * i <= j; i++) {
        if (j % i == 0) {
          x++;
        }
        if (j / i != i && j % (j / i) == 0) {
          x++;
        }
      }
      ans += j * x;
    }
    cout << ans << endl;
  }
}