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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const ll INF = 1000000000000000000LL;
const double EPS = 1e-8;
const int MOD = 1000000007;

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> res;
  for (int i = 2; (ll)i * i <= n; i++) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}
int main() {
  int N;
  string M;
  while (cin >> N >> M) {
    if (N == 0 && M == "0")
      break;
    if (M == "0") {
      cout << 0 << endl;
      continue;
    }
    map<ll, ll> factor = prime_factor(N);
    map<ll, ll> cnt;
    ll L = strtoll(M.c_str(), NULL, N);
    if (L == 0)
      while (true)
        ;
    ll ans = LLONG_MAX;
    for (map<ll, ll>::iterator it = factor.begin(); it != factor.end(); it++) {
      for (ll i = it->first; i <= L; i *= it->first) {
        cnt[it->first] += L / i;
        if (L / i < it->first)
          break;
      }
      cnt[it->first] /= it->second;
      ans = min(cnt[it->first], ans);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const ll INF = 1000000000000000000LL;
const double EPS = 1e-8;
const int MOD = 1000000007;

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> res;
  for (int i = 2; (ll)i * i <= n; i++) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}
int main() {
  int N;
  string M;
  while (cin >> N >> M) {
    if (N == 0 && M == "0")
      break;
    if (M == "0") {
      cout << 0 << endl;
      continue;
    }
    map<ll, ll> factor = prime_factor(N);
    map<ll, ll> cnt;
    ll L = strtoll(M.c_str(), NULL, N);
    if (M != "0" && L == 0)
      while (true)
        ;
    ll ans = INF;
    for (map<ll, ll>::iterator it = factor.begin(); it != factor.end(); it++) {
      for (ll i = it->first; i <= L; i *= it->first) {
        cnt[it->first] += L / i;
      }
      cnt[it->first] /= it->second;
      ans = min(cnt[it->first], ans);
    }
    cout << ans << endl;
  }
  return 0;
}