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

#include <bits/stdc++.h>
#include <cstdlib>
#include <numeric>
#define ll long long int
#define all(x) x.begin(), x.end()

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

#define v vector<long long int>
#define vv vector<int>
#define gcd(m, n) __gcd(m, n)
#define vp vector<pair<long long int, long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int, long long int>
#define f(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, n) for (ll i = a; i < n; i += 1)
#define fe(d, e) for (auto d : e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll, ll>
#define N 100005LL
using namespace std;

#define T (sizeof(arr) / sizeof(arr[0]))

const ll MAX = 1000001;

// array to store prime factors
ll 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 (ll i = 2; i < MAX; i++)
    factor[i] = i;

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

  int ans = 1;

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

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

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

auto solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  loop(i, 1, n) {
    ll x = calculateNoOFactors(n - i);
    ans += (x);
  }
  cout << ans << "\\n";
}

int main() {
  ll t = 1;
  generatePrimeFactors();

  while (t--)
    solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cstdlib>
#include <numeric>
#define ll long long int
#define all(x) x.begin(), x.end()

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

#define v vector<long long int>
#define vv vector<int>
#define gcd(m, n) __gcd(m, n)
#define vp vector<pair<long long int, long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int, long long int>
#define f(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, n) for (ll i = a; i < n; i += 1)
#define fe(d, e) for (auto d : e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll, ll>
#define N 100005LL
using namespace std;

#define T (sizeof(arr) / sizeof(arr[0]))

// Returns nCr % p using Fermat's little
// theorem.
void SieveOfEratosthenes(int n, bool prime[], bool primesquare[], int 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 (int 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 (int i = 0; i <= (n * n + 1); i++)
    primesquare[i] = false;

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

  for (int 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 (int i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  int j = 0;
  for (int 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];

  int 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.
    ll 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
}

vector<vector<ll>> graph(200005);
ll visited[200005];

void dfs(ll node) {
  visited[node] = 1;
  cout << node << " ";
  for (ll u : graph[node]) {
    if (!visited[u])
      dfs(u);
  }
}
ll factor(ll n) {
  ll cnt = 0;
  for (int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (i == n / i)
        cnt += 1;
      else
        cnt += 2;
    }
  }
  return cnt;
}

auto solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  loop(i, 1, n) {
    ll x = countDivisors(n - i);
    ans += (x);
  }
  cout << ans << "\\n";
}

int main() {
  ll t = 1;
  fastio;

  while (t--)
    solve();
}