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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

unsigned long long conv(int base, const string &fact) {
  unsigned long long res = 0;
  unsigned long long digit = 1ULL;
  for (int i = fact.size() - 1; i >= 0; i--) {
    if ('A' <= fact[i] && fact[i] <= 'Z') {
      res += (fact[i] - 'A' + 10) * digit;
      digit *= base;
    } else {
      res += (fact[i] - '0') * digit;
      digit *= base;
    }
  }
  return res;
}

int main() {
  int base;
  string fact;
  while (cin >> base >> fact) {
    if (base == 0 && fact == "0")
      break;
    unsigned long long init = conv(base, fact);
    unsigned long long prime_factor[37] = {};
    memset(prime_factor, 0, sizeof(prime_factor));
    for (int i = 2; i <= base; i++) {
      while (base % i == 0) {
        prime_factor[i]++;
        base /= i;
      }
    }

    unsigned long long res = numeric_limits<unsigned long long>::max();
    for (unsigned long long i = 2; i <= 36; i++) {
      if (prime_factor[i] == 0)
        continue;
      unsigned long long count = 0;
      unsigned long long tmp = init;

      while (tmp > 0) {
        count += tmp / i;
        tmp /= i;
      }
      res = min(res, count / prime_factor[i]);
    }

    if (res == numeric_limits<unsigned long long>::max())
      res = 0;
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

unsigned long long conv(int base, const string &fact) {
  unsigned long long res = 0;
  unsigned long long digit = 1;
  for (int i = fact.size() - 1; i >= 0; i--) {
    if ('A' <= fact[i] && fact[i] <= 'Z') {
      res += (fact[i] - 'A' + 10) * digit;
      digit *= base;
    } else {
      res += (fact[i] - '0') * digit;
      digit *= base;
    }
  }
  return res;
}

int main() {
  int base;
  string fact;
  while (cin >> base >> fact) {
    if (base == 0 && fact == "0")
      break;
    unsigned long long num = conv(base, fact);
    int count[6] = {};
    while (num > 0) {
      unsigned long long tmp = num;
      while (tmp % 2 == 0) {
        tmp /= 2;
        count[2]++;
      }
      while (tmp % 5 == 0) {
        tmp /= 5;
        count[5]++;
      }
      num--;
    }

    printf("%d\\n", min(count[2], count[5]));
  }
}