Problem p01209 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <vector>

const int MAX_P = 37;
using namespace std;

typedef unsigned long long ull;

vector<int> vprimes;
bool isPrime[MAX_P];

void MakePrime() {
  isPrime[0] = false;
  isPrime[1] = false;
  isPrime[2] = true;
  for (int i = 2; i < MAX_P; ++i) {
    isPrime[i] = true;
  }
  for (int i = 2; i < MAX_P; ++i) {
    if (isPrime[i]) {
      for (int j = 2; i * j < MAX_P; ++j) {
        isPrime[i * j] = false;
      }
    }
  }
  for (int i = 0; i < MAX_P; ++i) {
    if (isPrime[i]) {
      vprimes.push_back(i);
    }
  }
}

ull convtodec(string t, ull B) {
  ull ret = 0;
  for (int i = 0; i < (int)t.length(); ++i) {
    int d = 0;
    if (t[i] >= 'A' && t[i] <= 'Z')
      d = t[i] - 'A' + 10;
    else
      d = t[i] - '0';
    // cout << t[0] << ' ' << d << endl;
    ret *= B;
    ret += d;
  }
  return ret;
}

ull ipow(ull t, int p) {
  ull ret = 1;
  for (int i = 0; i < p; ++i) {
    ret *= t;
  }
  return ret;
}

int main() {
  MakePrime();
  ull N, B;
  ull base;
  string num;
  while (cin >> base >> num) {
    if (base == 0)
      break;
    if (base == 0 && num[0] == '0')
      break;

    N = convtodec(num, base);
    B = base;

    // cout << N << ' ' << B << endl;

    ull num[vprimes.size()];
    for (int i = 0; i < (int)vprimes.size(); ++i) {
      num[i] = 0;
      int maxpow = 1;
      while (N >= ipow(vprimes[i], maxpow)) {
        ++maxpow;
      }
      --maxpow;

      ull presum = 0;
      for (int p = maxpow; p >= 1; --p) {
        ull tnum = (N / ipow(vprimes[i], p)) - presum;
        presum += tnum;
        num[i] += p * tnum;
      }
      // cout << vprimes[i] << ' ' << num[i] << endl;
    }

    ull res = 1LL << 62;
    for (int i = 0; i < (int)vprimes.size(); ++i) {
      int e = 0;
      ull tB = B;
      while (tB % vprimes[i] == 0) {
        ++e;
        tB /= vprimes[i];
      }
      if (e > 0) {
        res = min(res, num[i] / e);
      }
    }
    if (res == 1LL << 62)
      res = 0;

    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <vector>

const int MAX_P = 37;
using namespace std;

vector<int> vprimes;
bool isPrime[MAX_P];

void MakePrime() {
  isPrime[0] = false;
  isPrime[1] = false;
  isPrime[2] = true;
  for (int i = 2; i < MAX_P; ++i) {
    isPrime[i] = true;
  }
  for (int i = 2; i < MAX_P; ++i) {
    if (isPrime[i]) {
      for (int j = 2; i * j < MAX_P; ++j) {
        isPrime[i * j] = false;
      }
    }
  }
  for (int i = 0; i < MAX_P; ++i) {
    if (isPrime[i]) {
      vprimes.push_back(i);
    }
  }
}

long long convtodec(string t, long long B) {
  long long ret = 0;
  for (int i = 0; i < (int)t.length(); ++i) {
    int d = 0;
    if (t[i] >= 'A' && t[i] <= 'Z')
      d = t[i] - 'A' + 10;
    else
      d = t[i] - '0';
    // cout << t[0] << ' ' << d << endl;
    ret *= B;
    ret += d;
  }
  return ret;
}

long long ipow(long long t, int p) {
  long long ret = 1;
  for (int i = 0; i < p; ++i) {
    ret *= t;
  }
  return ret;
}

int main() {
  MakePrime();
  long long int N, B;
  long long base;
  string num;
  while (cin >> base >> num) {
    if (base == 0 && num == "0")
      break;
    N = convtodec(num, base);
    B = base;

    if (N == 0) {
      printf("0\\n");
      continue;
    }

    long long num[vprimes.size()];
    for (int i = 0; i < (int)vprimes.size(); ++i) {
      num[i] = 0;
      int maxpow = 1;
      while (N > ipow(vprimes[i], maxpow)) {
        ++maxpow;
      }
      --maxpow;
      long long presum = 0;
      // cout << vprimes[i] << ' ' << maxpow << endl;
      for (int p = maxpow; p >= 1; --p) {
        long long tnum = (N / ipow(vprimes[i], p)) - presum;
        presum += tnum;
        num[i] += p * tnum;
      }
      // cout << num[i] << endl;
    }

    long long res = 1LL << 62;
    for (int i = 0; i < (int)vprimes.size(); ++i) {
      int e = 0;
      long long tB = B;
      while (tB % vprimes[i] == 0) {
        ++e;
        tB /= vprimes[i];
      }
      if (e > 0) {
        res = min(res, num[i] / e);
      }
    }
    if (res == 1LL << 62)
      res = 0;

    cout << res << endl;
  }
  return 0;
}