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

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define fi(i, start, end) for (int i = start; i < end; ++i)
#define fd(i, end, start) for (int i = end - 1; i >= start; --i)
#define pb push_back
#define mz(a) memset(a, 0, sizeof(a))
#define nl cout << endl;
#define ip(n) cin >> n;
#define op(n) cout << n;
#define FT()                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define inparr fi(i, 0, n) ip(a[i]);
#define dispar                                                                 \\
  fi(i, 0, n) op(a[i]);                                                        \\
  nl;
#define Fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
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;
      }
    }
  }
}

// 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;
}

void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  generatePrimeFactors();
  for (ll i = 1; i < n; i++) {
    ll no = n - i;
    ans += calculateNoOFactors(no);
  }

  cout << ans;
}
int main() {
  /*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
  #endif*/
  // FT()
  solve();

  /*#ifndef ONLINE_JUDGE
cout<<"\\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s";
#endif*/
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define fi(i, start, end) for (int i = start; i < end; ++i)
#define fd(i, end, start) for (int i = end - 1; i >= start; --i)
#define pb push_back
#define mz(a) memset(a, 0, sizeof(a))
#define nl cout << endl;
#define ip(n) cin >> n;
#define op(n) cout << n;
#define FT()                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define inparr fi(i, 0, n) ip(a[i]);
#define dispar                                                                 \\
  fi(i, 0, n) op(a[i]);                                                        \\
  nl;
#define Fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;

ll printDivisors(ll n) {
  ll cnt = 0;
  // Note that this loop runs till square root
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      // If divisors are equal, print only one
      if (n / i == i)
        cnt++;

      else // Otherwise print both
        cnt += 2;
    }
  }
  return cnt;
}

bool isPrime(ll n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i < n; i++) {
    ll no = n - i;
    if (isPrime(no)) {
      ans += 2;
    } else {
      ans += printDivisors(no);
    }
  }

  cout << ans;
}
int main() {
  /*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
  #endif*/
  // FT()
  solve();

  /*#ifndef ONLINE_JUDGE
cout<<"\\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s";
#endif*/
  return 0;
}