Problem p02660 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL ksc(LL a, LL n, LL mod) {
  LL ret = 0;
  for (; n; n >>= 1) {
    if (n & 1) {
      ret += a;
      if (ret >= mod)
        ret -= mod;
    }
    a <<= 1;
    if (a >= mod)
      a -= mod;
  }
  return ret;
}
LL ksm(LL a, LL n, LL mod) {
  LL ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ksc(ret, a, mod);
    a = ksc(a, a, mod);
  }
  return ret;
}
int millerRabin(LL n) {
  if (n < 2 || (n != 2 && !(n & 1)))
    return 0;
  LL d = n - 1;
  for (; !(d & 1); d >>= 1)
    ;
  for (int i = 0; i < 20; ++i) {
    LL a = rand() % (n - 1) + 1;
    LL t = d, m = ksm(a, d, n);
    for (; t != n - 1 && m != 1 && m != n - 1; m = ksc(m, m, n), t <<= 1)
      ;
    if (m != n - 1 && !(t & 1))
      return 0;
  }
  return 1;
}
LL cnt, fact[100];
LL gcd(LL a, LL b) { return !b ? a : gcd(b, a % b); }
LL pollardRho(LL n, int a) {
  LL x = rand() % n, y = x, d = 1, k = 0, i = 1;
  while (d == 1) {
    ++k;
    x = ksc(x, x, n) + a;
    if (x >= n)
      x -= n;
    d = gcd(x > y ? x - y : y - x, n);
    if (k == i) {
      y = x;
      i <<= 1;
    }
  }
  if (d == n)
    return pollardRho(n, a + 1);
  return d;
}
void findfac(LL n) {
  if (millerRabin(n)) {
    fact[++cnt] = n;
    return;
  }
  LL p = pollardRho(n, rand() % (n - 1) + 1);
  findfac(p);
  findfac(n / p);
}
LL l, ans;
LL n;
LL a[100], b[100];
int main() {
  scanf("%lld", &n);
  findfac(n);
  sort(fact + 1, fact + 1 + cnt);
  for (int i = 1; i <= cnt; ++i) {
    if (fact[i] != fact[i - 1]) {
      ++l;
      a[l] = fact[i];
      ++b[l];
    } else
      ++b[l];
  }
  for (int i = 1; i <= l; ++i) {
    for (int j = 1; j <= 100; ++j)
      if (b[i] >= j) {
        ++ans;
        b[i] -= j;
      } else
        break;
  }
  printf("%lld", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL ksc(LL a, LL n, LL mod) {
  LL ret = 0;
  for (; n; n >>= 1) {
    if (n & 1) {
      ret += a;
      if (ret >= mod)
        ret -= mod;
    }
    a <<= 1;
    if (a >= mod)
      a -= mod;
  }
  return ret;
}
LL ksm(LL a, LL n, LL mod) {
  LL ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ksc(ret, a, mod);
    a = ksc(a, a, mod);
  }
  return ret;
}
int millerRabin(LL n) {
  if (n < 2 || (n != 2 && !(n & 1)))
    return 0;
  LL d = n - 1;
  for (; !(d & 1); d >>= 1)
    ;
  for (int i = 0; i < 20; ++i) {
    LL a = rand() % (n - 1) + 1;
    LL t = d, m = ksm(a, d, n);
    for (; t != n - 1 && m != 1 && m != n - 1; m = ksc(m, m, n), t <<= 1)
      ;
    if (m != n - 1 && !(t & 1))
      return 0;
  }
  return 1;
}
LL cnt, fact[100];
LL gcd(LL a, LL b) { return !b ? a : gcd(b, a % b); }
LL pollardRho(LL n, int a) {
  LL x = rand() % n, y = x, d = 1, k = 0, i = 1;
  while (d == 1) {
    ++k;
    x = ksc(x, x, n) + a;
    if (x >= n)
      x -= n;
    d = gcd(x > y ? x - y : y - x, n);
    if (k == i) {
      y = x;
      i <<= 1;
    }
  }
  if (d == n)
    return pollardRho(n, a + 1);
  return d;
}
void findfac(LL n) {
  if (millerRabin(n)) {
    fact[++cnt] = n;
    return;
  }
  LL p = pollardRho(n, rand() % (n - 1) + 1);
  findfac(p);
  findfac(n / p);
}
LL l, ans;
LL n;
LL a[100], b[100];
int main() {
  scanf("%lld", &n);
  if (n == 1) {
    printf("0");
    return 0;
  }
  findfac(n);
  sort(fact + 1, fact + 1 + cnt);
  for (int i = 1; i <= cnt; ++i) {
    if (fact[i] != fact[i - 1]) {
      ++l;
      a[l] = fact[i];
      ++b[l];
    } else
      ++b[l];
  }
  for (int i = 1; i <= l; ++i) {
    for (int j = 1; j <= 100; ++j)
      if (b[i] >= j) {
        ++ans;
        b[i] -= j;
      } else
        break;
  }
  printf("%lld", ans);
}