Problem p02548 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vii;
const double pi = 2 * acos(0);

#define pf printf
#define sf scanf
#define pb(a) push_back(a)
#define mp make_pair
#define ff first
#define ss second
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define forab(i, a, b) for (int i = a; i <= b; i++)
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))
#define sq(a) (a) * (a)
#define endl "\\n"
#define abs(x) fabs(x)
#define all(v) v.begin(), v.end()
#define pcase(z, x) cout << "Case " << z << ": " << x << "\\n"
#define mod 1000000007
#define N 1000007
#define WakandaForever                                                         \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
bool p[1000007];
void sieve() {
  for (ll j = 1; j <= N; j++)
    p[j] = 1;
  for (ll x = 2; x * x <= N; x++) {
    for (ll i = x * x; i <= N; i += x) {
      p[i] = 0;
    }
  }
}
ll nod(ll n) {
  ll c, total = 1;
  c = 0;
  while (n % 2 == 0) {
    c++;
    n /= 2;
  }
  total *= (c + 1);
  for (ll i = 3; i * i <= n; i += 2) {
    if (p[i] && n % i == 0) {
      c = 0;
      while (n % i == 0) {
        c++;
        n /= i;
      }
      total *= (c + 1);
    }
  }
  if (n > 2)
    total *= 2;
  return total;
}
int main() {
  WakandaForever sieve();
  ll n, cnt = 0;
  cin >> n;
  ll c = 1;
  while (n - c > 0) {
    ll k = n - c;
    ll x = nod(k);
    cnt += x;
    c++;
  }
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vii;
const double pi = 2 * acos(0);

#define pf printf
#define sf scanf
#define pb(a) push_back(a)
#define mp make_pair
#define ff first
#define ss second
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define forab(i, a, b) for (int i = a; i <= b; i++)
#define lcm(a, b) ((a) * ((b) / gcd(a, b)))
#define sq(a) (a) * (a)
#define endl "\\n"
#define abs(x) fabs(x)
#define all(v) v.begin(), v.end()
#define pcase(z, x) cout << "Case " << z << ": " << x << "\\n"
#define mod 1000000007
#define WakandaForever                                                         \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
void SieveOfEratosthenes(ll n, bool prime[], bool primesquare[], ll a[]) {
  // Create a boolean array "prime[0..n]" and
  // initialize all entries it as true. A value
  // in prime[i] will finally be false if i is
  // Not a prime, else true.
  for (ll i = 2; i <= n; i++)
    prime[i] = true;

  // Create a boolean array "primesquare[0..n*n+1]"
  // and initialize all entries it as false. A value
  // in squareprime[i] will finally be true if i is
  // square of prime, else false.
  for (ll i = 0; i <= (n * n + 1); i++)
    primesquare[i] = false;

  // 1 is not a prime number
  prime[1] = false;

  for (ll p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then
    // it is a prime
    if (prime[p] == true) {
      // Update all multiples of p
      for (ll i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  ll j = 0;
  for (ll p = 2; p <= n; p++) {
    if (prime[p]) {
      // Storing primes in an array
      a[j] = p;

      // Update value in primesquare[p*p],
      // if p is prime.
      primesquare[p * p] = true;
      j++;
    }
  }
}

// Function to count divisors
ll countDivisors(ll n) {
  // If number is 1, then it will have only 1
  // as a factor. So, total factors will be 1.
  if (n == 1)
    return 1;

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

  ll a[n]; // for storing primes upto n

  // Calling SieveOfEratosthenes to store prime
  // factors of n and to store square of prime
  // factors of n
  SieveOfEratosthenes(n, prime, primesquare, a);

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

  // Loop for counting factors of n
  for (ll 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.
    int 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

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

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

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

  return ans; // Total divisors
}
int main() {
  WakandaForever ll n, cnt = 0;
  cin >> n;
  ll c = 1;
  while (n - c > 0) {
    ll k = n - c;
    ll x = countDivisors(k);
    cnt += x;
    c++;
  }
  cout << cnt << endl;
  return 0;
}