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

/*
    __  __               __                    __      __               __
______      __           __ / / / /___ __________/ /      ______  _____/ /__   /
/_  ___  ____ _/ /______   /_  __/___ _/ /__  ____  / /_ / /_/ / __ `/ ___/ __
/ | /| / / __ \\/ ___/ //_/  / __ \\/ _ \\/ __ `/ __/ ___/    / / / __ `/ / _ \\/ __
\\/ __/ / __  / /_/ / /  / /_/ /| |/ |/ / /_/ / /  / ,<    / /_/ /  __/ /_/ /
/_(__  )    / / / /_/ / /  __/ / / / /_
/_/ /_/\\__,_/_/   \\__,_/ |__/|__/\\____/_/  /_/|_|  /_.___/\\___/\\__,_/\\__/____/
/_/  \\__,_/_/\\___/_/ /_/\\__/

*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0)
const int mod = 1000000007;
// ll fun(string a)
// {
//     ll num=0;
//     ll mul=1;
//     ll i,j;
//     for(i=0;i<a.size();i++)
//     {
//         num+=(a[a.size()-1-i]-'0')*mul;
//         mul=mul*10;
//     }
//     return num;
// }
map<ll, vector<ll>> fact;
// void fun(ll num)
// {
//     ll i,j;
//     for(ll num=1;j<=n;j++)
//     {
//         for(i=1;i*i<=num;i++)
//         {
//             if(num%i==0)
//             {
//                 if(i==num/i)
//                 {
//                     fact[num].push_back(i);
//                 }
//                 else
//                 {
//                     fact[num].push_back(i);
//                     fact[num].push_back(num/i);
//                 }
//             }
//         }
//     }
// }
const int MAX = 1000001;

// 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;
      }
    }
  }
}
int main() {
  ll n;
  cin >> n;
  ll i, j;

  generatePrimeFactors();
  ll gans = 0;
  for (ll num = 1; num <= n - 1; num++) {

    int ans = 1;
    int dup = factor[num];

    // 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 = num / factor[num];

    // 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);
    gans += ans;
  }

  // cout<<endl;
  cout << gans - 1 << endl;
}


Error: TLE

Fixed submission:

/*
    __  __               __                    __      __               __
______      __           __ / / / /___ __________/ /      ______  _____/ /__   /
/_  ___  ____ _/ /______   /_  __/___ _/ /__  ____  / /_ / /_/ / __ `/ ___/ __
/ | /| / / __ \\/ ___/ //_/  / __ \\/ _ \\/ __ `/ __/ ___/    / / / __ `/ / _ \\/ __
\\/ __/ / __  / /_/ / /  / /_/ /| |/ |/ / /_/ / /  / ,<    / /_/ /  __/ /_/ /
/_(__  )    / / / /_/ / /  __/ / / / /_
/_/ /_/\\__,_/_/   \\__,_/ |__/|__/\\____/_/  /_/|_|  /_.___/\\___/\\__,_/\\__/____/
/_/  \\__,_/_/\\___/_/ /_/\\__/

*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0)
const int mod = 1000000007;
// ll fun(string a)
// {
//     ll num=0;
//     ll mul=1;
//     ll i,j;
//     for(i=0;i<a.size();i++)
//     {
//         num+=(a[a.size()-1-i]-'0')*mul;
//         mul=mul*10;
//     }
//     return num;
// }
map<ll, vector<ll>> fact;
// void fun(ll num)
// {
//     ll i,j;
//     for(ll num=1;j<=n;j++)
//     {
//         for(i=1;i*i<=num;i++)
//         {
//             if(num%i==0)
//             {
//                 if(i==num/i)
//                 {
//                     fact[num].push_back(i);
//                 }
//                 else
//                 {
//                     fact[num].push_back(i);
//                     fact[num].push_back(num/i);
//                 }
//             }
//         }
//     }
// }
int main() {
  ll n;
  cin >> n;
  ll i, j;

  ll ans = 0;
  for (ll num = 1; num <= n - 1; num++) {
    ll cnt = 0;
    for (i = 1; i * i <= num; i++) {
      if (num % i == 0) {
        if (i == num / i) {
          cnt++;
        } else {
          cnt += 2;
        }
      }
    }
    ans += cnt;
  }

  // cout<<endl;
  cout << ans << endl;
}