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

#include <bits/stdc++.h>
using namespace std;

const long long MAX = 10000001;

// array to store prime factors
long long factor[MAX] = {0};

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

  // Initializes all the positions with their value.
  for (long long i = 2; i < MAX; i++)
    factor[i] = i;

  // Initializes all multiples of 2 with 2
  for (long long i = 4; i < MAX; i += 2)
    factor[i] = 2;

  // A modified version of Sieve of Eratosthenes to
  // store the smallest prime factor that divides
  // every number.
  for (long long i = 3; i * i < MAX; i++) {
    // check if it has no prime factor.
    if (factor[i] == i) {
      // Initializes of j starting from i*i
      for (long long j = i * i; j < MAX; j += i) {
        // if it has no prime factor before, then
        // stores the smallest prime divisor
        if (factor[j] == j)
          factor[j] = i;
      }
    }
  }
}

// function to calculate number of factors
long long calculateNoOFactors(long long n) {
  if (n == 1)
    return 1;

  long long ans = 1;

  // stores the smallest prime number
  // that divides n
  long long dup = factor[n];

  // stores the count of number of times
  // a prime number divides n.
  long long c = 1;

  // reduces to the next number after prime
  // factorization of n
  long long j = n / factor[n];

  // false when prime factorization is done
  while (j != 1) {
    // if the same prime number is dividing n,
    // then we increase the count
    if (factor[j] == dup)
      c += 1;

    /* if its a new prime factor that is factorizing n,
       then we again set c=1 and change dup to the new
       prime factor, and apply the formula explained
       above. */
    else {
      dup = factor[j];
      ans = ans * (c + 1);
      c = 1;
    }

    // prime factorizes a number
    j = j / factor[j];
  }

  // for the last prime factor
  ans = ans * (c + 1);

  return ans;
}

int main() {
  long long n;
  cin >> n;
  long long ans = 0;

  generatePrimeFactors();

  for (long long i = 1; i <= n; i++) {
    ans += i * calculateNoOFactors(i);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

void SieveOfEratosthenes(long long n, bool prime[], bool primesquare[],
                         long long a[]) {
  for (long long i = 2; i <= n; i++)
    prime[i] = true;

  for (long long i = 0; i <= (n * n + 1); i++)
    primesquare[i] = false;

  prime[1] = false;

  for (long long p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  long long j = 0;
  for (long long p = 2; p <= n; p++) {
    if (prime[p]) {
      a[j] = p;
      primesquare[p * p] = true;
      j++;
    }
  }
}

// Function to count divisors
long long countDivisors(long long n) {
  if (n == 1)
    return 1;

  bool prime[n + 1], primesquare[n * n + 1];

  long long a[n];
  SieveOfEratosthenes(n, prime, primesquare, a);
  long long ans = 1;

  for (long long i = 0;; i++) {
    if (a[i] * a[i] * a[i] > n)
      break;

    long long cnt = 1;
    while (n % a[i] == 0) {
      n = n / a[i];
      cnt = cnt + 1;
    }

    ans = ans * cnt;
  }

  if (prime[n])
    ans = ans * 2;

  else if (primesquare[n])
    ans = ans * 3;

  else if (n != 1)
    ans = ans * 4;

  return ans;
}

/*
long long countDivisors(long long n)
{
    long long cnt = 0;
    for (long long i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
            // If divisors are equal,
            // count only one
            if (n / i == i)
                cnt++;

            else // Otherwise count both
                cnt = cnt + 2;
        }
    }
    return cnt;
}
*/

int main() {
  long long n;
  cin >> n;
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    ans += i * countDivisors(i);
  }
  cout << ans << endl;
}