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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int NUM_P = 11;
const int pnums[NUM_P] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};

const unsigned long long LINF = ~1ULL;

/* typedef */

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<pii> vpii;

/* global variables */

int n;
ull m;

/* subroutines */

inline int ch2int(char ch) {
  if (ch >= '0' && ch <= '9')
    return ch - '0';
  return ch - 'A' + 10;
}

void prime_decomp(int k, vpii &pds) {
  pds.clear();

  for (int pi = 0; k > 1; pi++) {
    int pn = pnums[pi];
    if (k % pn == 0) {
      int fi = 0;
      while (k % pn == 0)
        k /= pn, fi++;
      pds.push_back(pii(pn, fi));
    }
  }
}

void print_pds(vpii &pds) {
  for (int i = 0; i < pds.size(); i++) {
    if (i > 0)
      cout << '*';
    cout << pds[i].first << '^' << pds[i].second;
  }
  cout << endl;
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    string mstr;
    cin >> mstr;

    m = 0;
    for (int i = 0; i < mstr.length(); i++)
      m = m * n + ch2int(mstr[i]);
    // cout << m << endl;

    vpii pds;
    prime_decomp(n, pds);
    // print_pds(pds);

    ull min_numz = LINF;

    for (int i = 0; i < pds.size(); i++) {
      int pn = pds[i].first;
      ull pns = 0;
      for (ull pexp = pn; pexp <= m; pexp *= pn)
        pns += m / pexp;
      // printf("%lld: %d = %lld\\n", m, pn, pns);

      ull numz = pns / pds[i].second;
      if (min_numz > numz)
        min_numz = numz;
    }

    cout << min_numz << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int NUM_P = 11;
const int pnums[NUM_P] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};

const long long LINF = 1LL << 62;

/* typedef */

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<pii> vpii;

/* global variables */

int n;
ll m;

/* subroutines */

inline int ch2int(char ch) {
  if (ch >= '0' && ch <= '9')
    return ch - '0';
  return ch - 'A' + 10;
}

void prime_decomp(int k, vpii &pds) {
  pds.clear();

  for (int pi = 0; k > 1; pi++) {
    int pn = pnums[pi];
    if (k % pn == 0) {
      int fi = 0;
      while (k % pn == 0)
        k /= pn, fi++;
      pds.push_back(pii(pn, fi));
    }
  }
}

void print_pds(vpii &pds) {
  for (int i = 0; i < pds.size(); i++) {
    if (i > 0)
      cout << '*';
    cout << pds[i].first << '^' << pds[i].second;
  }
  cout << endl;
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    string mstr;
    cin >> mstr;

    m = 0;
    for (int i = 0; i < mstr.length(); i++)
      m = m * n + ch2int(mstr[i]);
    // cout << m << endl;

    vpii pds;
    prime_decomp(n, pds);
    // print_pds(pds);

    ll min_numz = LINF;

    for (int i = 0; i < pds.size(); i++) {
      int pn = pds[i].first;
      ll pns = 0;
      for (ll pexp = pn; pexp <= m; pexp *= pn)
        pns += m / pexp;
      // printf("%lld: %d = %lld\\n", m, pn, pns);

      ll numz = pns / pds[i].second;
      if (min_numz > numz)
        min_numz = numz;
    }

    cout << min_numz << endl;
  }

  return 0;
}