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

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

bool prime[1000002];
lli a[1000002];

void SieveOfEratosthenes(lli n, bool prime[], lli a[]) {

  for (lli i = 2; i <= n; i++)
    prime[i] = true;

  prime[1] = false;

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

  lli j = 0;
  for (lli p = 2; p <= n; p++) {
    if (prime[p]) {
      a[j] = p;
      j++;
    }
  }
}

lli countDivisors(lli n) {

  if (n == 1)
    return 1;

  // ans will contain total number of distinct
  // divisors
  lli ans = 1;

  // Loop for counting factors of n
  for (lli i = 0;; i++) {
    // a[i] is not less than cube root n
    if (a[i] * a[i] * a[i] > n)
      break;

    // Calculating power of a[i] in n.
    lli cnt = 1;          // cnt is power of prime a[i] in n.
    while (n % a[i] == 0) // if a[i] is a factor of n
    {
      n = n / a[i];
      cnt = cnt + 1; // incrementing power
    }

    // Calculating the number of divisors
    // If n = a^p * b^q then total divisors of n
    // are (p+1)*(q+1)
    ans = ans * cnt;
  }

  // if a[i] is greater than cube root of n
  int r = sqrt(n);
  double r1 = sqrt(n);
  // First case
  if (prime[n])
    ans = ans * 2;

  // Second case
  else if (r1 - floor(r1) == 0 && prime[r])
    ans = ans * 3;

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

  return ans; // Total divisors
}

int main() {
  fast;
  int t = 1;
  while (t--) {
    lli n;
    cin >> n;
    lli cnt = 0;
    SieveOfEratosthenes(n, prime, a);
    for (int i = 1; i < n; i++) {
      cnt += countDivisors(n - i);
      // cout << cnt << " ";
    }
    cout << cnt << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

vector<bool> isPrime(1000005, 1);

void seive(int N) {
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i * i <= N; ++i) {
    if (isPrime[i] == true) { // Mark all the multiples of i as composite
                              // numbers
      for (int j = i * i; j <= N; j += i)
        isPrime[j] = false;
    }
  }
}

lli divCount(int n) {

  // Traversing through all prime numbers
  lli total = 1;
  for (int p = 2; p <= n; p++) {
    if (isPrime[p]) {

      int count = 0;
      if (n % p == 0) {
        while (n % p == 0) {
          n = n / p;
          count++;
        }
        total = total * (count + 1);
      }
    }
  }
  return total;
}

int main() {
  fast;
  int t = 1;
  while (t--) {
    lli n;
    cin >> n;
    lli cnt = 0;
    seive(n);
    for (int i = 1; i < n; i++) {
      cnt += divCount(n - i);
      // cout << cnt << " ";
    }
    cout << cnt << "\\n";
  }
  return 0;
}