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

#include <stdio.h>
#include <stdlib.h>
long long int SieveNbOfDiv(long long int NumberOfDivisors[],
                           long long int IndexCount[], long long int Limit) {
  for (long long int i = 1; i * i <= Limit; i++) {
    NumberOfDivisors[i * i] += 1;
    for (long long int j = i * (i + 1); j <= Limit; j += i)
      NumberOfDivisors[j] += 2;
  }
  long long int Count = 0, p = 0;
  for (int i = 1; i <= Limit; i++) {
    Count = 0;
    Count += NumberOfDivisors[i];
    NumberOfDivisors[i] = Count;
    IndexCount[i] = Count;
    p = p + Count * i;
  }
  return p;
}

/*void SieveDivisors(long long int IndexCount[], long long int
NumberOfDivisors[], long long int Divisors[], long long int Limit) { for (long
long int i = 1; i <= Limit; i++) { Divisors[IndexCount[i-1]++] = 1;
        Divisors[IndexCount[i]-1] = i;
    }
    for (long long int i = 2; i*i <= Limit; i++) {
        Divisors[IndexCount[i*i-1]++] = i;
        for (int j = i*(i+1); j <= Limit; j += i ) {
            Divisors[IndexCount[j-1]++] = i;
            Divisors[NumberOfDivisors[j-1] + NumberOfDivisors[j] -
IndexCount[j-1]] = j/i;
        }
    }
}*/
int main() {
  long long int N;
  scanf("%lld", &N);
  long long int *NumberOfDivisors =
      (long long int *)calloc(N + 1, sizeof(long long int));
  long long int *IndexCount =
      (long long int *)calloc(N + 1, sizeof(long long int));
  long long int size = SieveNbOfDiv(NumberOfDivisors, IndexCount, N);

  printf("%lld", size);
  /* long long int *Divisors = (long long int*) calloc(size+1, sizeof(long long
   int)); SieveDivisors(IndexCount, NumberOfDivisors, Divisors, N);*/
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
long long int SieveNbOfDiv(long long int NumberOfDivisors[],
                           long long int IndexCount[], long long int Limit) {
  for (long long int i = 1; i * i <= Limit; i++) {
    NumberOfDivisors[i * i] += 1;
    for (long long int j = i * (i + 1); j <= Limit; j += i)
      NumberOfDivisors[j] += 2;
  }
  long long int Count = 0, p = 0;
  for (int i = 1; i <= Limit; i++) {
    Count = 0;
    Count += NumberOfDivisors[i];
    NumberOfDivisors[i] = Count;
    IndexCount[i] = Count;
    p = p + Count * i;
  }
  return p;
}

void SieveDivisors(long long int IndexCount[], long long int NumberOfDivisors[],
                   long long int Divisors[], long long int Limit) {
  for (long long int i = 1; i <= Limit; i++) {
    Divisors[IndexCount[i - 1]++] = 1;
    Divisors[IndexCount[i] - 1] = i;
  }
  for (long long int i = 2; i * i <= Limit; i++) {
    Divisors[IndexCount[i * i - 1]++] = i;
    for (int j = i * (i + 1); j <= Limit; j += i) {
      Divisors[IndexCount[j - 1]++] = i;
      Divisors[NumberOfDivisors[j - 1] + NumberOfDivisors[j] -
               IndexCount[j - 1]] = j / i;
    }
  }
}
int main() {
  long long int N;
  scanf("%lld", &N);
  long long int *NumberOfDivisors =
      (long long int *)calloc(N + 1, sizeof(long long int));
  long long int *IndexCount =
      (long long int *)calloc(N + 1, sizeof(long long int));
  long long int size = SieveNbOfDiv(NumberOfDivisors, IndexCount, N);

  printf("%lld", size);
  long long int *Divisors =
      (long long int *)calloc(size + 1, sizeof(long long int));
  SieveDivisors(IndexCount, NumberOfDivisors, Divisors, N);
}