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

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

typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define MOD 1000000007
#define int ll

// int main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
//     ll n;cin>>n;
//     map<ll,ll>mp[n+1];
//     mp[1][]
//     ll sum=1;
//     rep(i,2,n+1){
//         ll tmp=i;ll cnt=0,tm=1;
//             for(ll j=2;j*j<=i;j++){
//                 cnt=0;
//                 while(tmp%j==0){
//                     tmp/=j;
//                     cnt++;
//                 }
//                 tm*=(cnt+1);
//                 if(tmp==1)break;
//             }
//             if(tmp!=1)tm*=2;
//             //cout<<i<<" :"<<tm<<"\\n";
//             sum+=tm*i;
//     }
//     cout<<sum;

// return 0;
// }

// C++ program to count number of factors
// of an array of integers

const int MAX = 10000001;

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

// function to generate all prime factors
// of numbers from 1 to 10^6
void generatePrimeFactors() {
  factor[1] = 1;

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

  // Initializes all multiples of 2 with 2
  for (int 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 (int 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 (int 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
int calculateNoOFactors(int n) {
  if (n == 1)
    return 1;

  int ans = 1;

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

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

  // reduces to the next number after prime
  // factorization of n
  int 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;
}

// Driver program to test above function
signed main() {
  // generate prime factors of number
  // upto 10^6
  generatePrimeFactors();
  ll n;
  cin >> n;
  ll sum = 0;
  for (int i = 1; i < n + 1; i++)
    sum += i * calculateNoOFactors(i);
  cout << sum;

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define MOD 1000000007

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  ll sum = 1;
  rep(i, 2, n + 1) {
    ll tmp = i;
    ll cnt = 0, tm = 1;
    for (ll j = 2; j * j <= i; j++) {
      cnt = 0;
      while (tmp % j == 0) {
        tmp /= j;
        cnt++;
      }
      tm *= (cnt + 1);
      if (tmp == 1)
        break;
    }
    if (tmp != 1)
      tm *= 2;
    // cout<<i<<" :"<<tm<<"\\n";
    sum += tm * i;
  }
  cout << sum;

  return 0;
}