Problem:
Problem C:
!
You are one of ICPC participants and in charge of developing a library for multiprecision numbers
and radix conversion. You have just finished writing the code, so next you have to test if it
works correctly. You decided to write a simple, well-known factorial function for this purpose:
Your task is to write a program that shows the number of trailing zeros when you compute
M
!
in base
N
, given
N
and
M
.
Input
The input contains multiple data sets. Each data set is described by one line in the format
below:
N M
where
N
is a decimal number between 8 and 36 inclusive, and
M
is given in the string repre-
sentation in base
N
. Exactly one white space character appears between them.
The string representation of
M
contains up to 12 characters in base
N
. In case
N
is greater
than 10, capital letters A, B, C, ... may appear in the string representation, and they represent
10, 11, 12, ..., respectively.
The input is terminated by a line containing two zeros. You should not process this line.
Output
For each data set, output a line containing a decimal integer, which is equal to the number of
trailing zeros in the string representation of
M
! in base
N
.
Sample Input
10 500
16 A
0 0
Output for the Sample Input
124
2

Submitted solutions:

code_snippet: #include<iostream>
#include<vector>
#include<climits>
using namespace std;
typedef long long ll;
#define inRange(x,a,b) (a <= x && x <= b)

int main(){
    int n;
    string m;
    vector<int> p({2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31});
    while(cin >> n >> m, n){
        vector<int> base(11, 0);
        vector<ll> cnt(11, 0);
        int cp = n;
        for(int i = 0; i < 11; i++){
            while(cp%p[i] == 0)  base[i]++, cp /= p[i];
        }
        ll val = 0;
        for(int i = 0; i < m.length(); i++){
            val *= n;
            if(inRange(m[i],'0','9'))   val += m[i]-'0';
            else                        val += 10+m[i]-'A';
        }
        for(int i = 0; i < 11; i++){
            ll x = val;
            while(x){
                cnt[i] += x/p[i];
                x /= p[i];
            }
        }

        ll ans = LLONG_MAX;
        for(int i = 0; i < 11; i++){
            if(base[i] == 0)    continue;
            ans = min(ans, cnt[i]/base[i]);
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3140
final_rank: 7

code_snippet: #include<iostream>
#include<vector>
#include<climits>
using namespace std;
typedef unsigned long long ll;
#define inRange(x,a,b) (a <= x && x <= b)

int main(){
    int n;
    string m;
    vector<int> p({2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31});
    while(cin >> n >> m, n){
        vector<int> base(11, 0);
        vector<ll> cnt(11, 0);
        int cp = n;
        for(int i = 0; i < 11; i++){
            while(cp%p[i] == 0)  base[i]++, cp /= p[i];
        }
        ll val = 0;
        for(int i = 0; i < m.length(); i++){
            val *= n;
            if(inRange(m[i],'0','9'))   val += m[i]-'0';
            else                        val += 10+m[i]-'A';
        }
        for(int i = 0; i < 11; i++){
            ll x = val;
            while(x){
                cnt[i] += x/p[i];
                x /= p[i];
            }
        }

        ll ans = LLONG_MAX;
        for(int i = 0; i < 11; i++){
            if(base[i] == 0)    continue;
            ans = min(ans, cnt[i]/base[i]);
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3000
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<cctype>
#include<climits>
using namespace std;
typedef unsigned long long ll;
#define inRange(x,a,b) (a <= x && x <= b)

int main(){
    int n;
    string m;
    vector<ll> p({2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31});
    while(cin >> n >> m, n){
        vector<ll> base(11, 0);
        vector<ll> cnt(11, 0);
        int cp = n;
        for(int i = 0; i < 11; i++){
            while(cp%p[i] == 0)  base[i]++, cp /= p[i];
        }
        
        ll val = 0;
        for(int i = 0; i < m.length(); i++){
            val *= n;
            if(isdigit(m[i]))   val += m[i]-'0';
            else                val += 10+m[i]-'A';
        }
        for(int i = 0; i < 11; i++){
            ll x = val;
            while(x){
                cnt[i] += x/p[i];
                x /= p[i];
            }
        }

        ll ans = LLONG_MAX;
        for(int i = 0; i < 11; i++){
            if(base[i] == 0)    continue;
            ans = min(ans, cnt[i]/base[i]);
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3156
final_rank: 18

code_snippet: #include<iostream>
#include<vector>
#include<climits>
#include<map>
using namespace std;
typedef long long ll;
#define inRange(x,a,b) (a <= x && x <= b)

int main(){
    ll n;
    string m;
    while(cin >> n >> m, n){
        ll val = 0;
        for(int i = 0; i < m.length(); i++){
            val *= n;
            if(inRange(m[i],'0','9'))   val += m[i]-'0';
            else                        val += 10+m[i]-'A';
        }
        map<int,int> m;
        for(int i = 2; i*i <= n; i++){
            while(n%i == 0) m[i]++, n /= i;
        }
        if(n != 1)  m[n]++;

        ll ans = LLONG_MAX;
        for(auto it = m.begin(); it != m.end(); it++){
            ll cnt = 0, cp = val;
            while(cp){
                cnt += cp/it->first;
                cp /= it->first;
            }
            ans = min(ans, cnt/it->second);
        }

        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3052
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;

template<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b) a=b;};


signed main(){
  Int n;
  string s;
  
  while(cin>>n>>s,n){
    using ull = unsigned long long;
    ull m=0;

    auto calc=[](char c){if(isdigit(c)) return c-'0';return c-'A'+10;};
  
    for(Int i=0;i<(Int)s.size();i++){
      m=m*n+calc(s[i]);
    }

    using P = pair<Int, Int>;
    vector<P> vp;
    Int k=n;
    for(Int i=2;i<=50;i++){
      if(k%i) continue;
      vp.emplace_back(i,0);
      while(k%i==0) k/=i,vp.back().second++;
    }
  
    ull ans=m;

    for(auto p:vp){
      ull k=p.first;
      ull res=0;
      while(k<=m){
	res+=m/k;
	if(k>=ULLONG_MAX/p.first-1) break;
	k*=p.first;
      }
      chmin(ans,res/p.second);
    }
  
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3212
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define int unsigned long long
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;

 
typedef pair<int,int>P;
 
int sosu[]={2,3,5,7,11,13,17,19,23,29,31,37},so=12;
 
int con(int n,string s){
  int res=0;
  for(int i=0;i<s.size();i++){
 
    res*=n;
     
    if(isalpha(s[i])){
      res+=(s[i]-'A')+10;
    }
    else{
      res+=(s[i]-'0');
    }
 
  }
  return res;
}
 
vector<int> bunkai(int n){
  vector<int>v;
  r(i,so){
    while(n%sosu[i]==0){
      n/=sosu[i];
      v.push_back(sosu[i]);
    }
  }
  return v;
}
 
signed main(){
 int N;
  string s;
  while( cin>>N>>s , N ){
    int n=N;
    int p=con(n,s);//cout<<p<<endl;
 
    vector<int>v=bunkai(n);
     
    vector<P>vec,vel;
 
    r(i,v.size()){
      int res=0;
      __int128 x=1;
      if(i&&v[i-1]==v[i])continue;
      __int128 pp=p;
      for(int j=1;;j++){
     
        x*=v[i];
 
        if(x>pp)break;
     
        res+=p/x;
      }
      vec.push_back(P(res,v[i]));
    }
     
    r(i,so){
      int res=0;
      r(j,v.size()){
        if(sosu[i]==v[j])res++;
      }
      if(res==0)continue;
      //cout<<sosu[i]<<' '<<res<<endl;
      //cout<<p/sosu[i]<<endl;
      vel.push_back(P(res,sosu[i]));
    }
 
    unsigned long long ans=ULLONG_MAX;
    r(i,vec.size()){
      ans=min(ans,(unsigned long long)(vec[i].first/vel[i].first));
    }
 
    cout<<ans<<endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3008
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;

template<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b) a=b;};


signed main(){
  int n;
  string s;
  
  while(cin>>n>>s,n){
    using ull = unsigned long long;
    ull m=0;

    auto calc=[](char c){if(isdigit(c)) return c-'0';return c-'A'+10;};
  
    for(int i=0;i<(int)s.size();i++){
      m=m*n+calc(s[i]);
    }

    using P = pair<int, int>;
    vector<P> vp;
    int k=n;
    for(int i=2;i<=50;i++){
      if(k%i) continue;
      vp.emplace_back(i,0);
      while(k%i==0) k/=i,vp.back().second++;
    }
  
    ull ans=m;

    for(auto p:vp){
      ull k=p.first;
      ull res=0;
      while(k<=m){
	res+=m/k;
	if(k>=ULLONG_MAX/p.first-1) break;
	k*=p.first;
      }
      chmin(ans,res/p.second);
    }
  
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3160
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef pair<ll,ll> P;
 
vector<P> D(ll n) {
  vector<P> v;
  for(ll i=2; i*i<=n; i++) {
    P p=P(0,i);
    while(n%i==0) {
      n/=i;
      p.F++;
    }
    if(p.F) v.push_back(p);
  }
  if(n>1) v.push_back(P(1,n));
  return v;
}
 
ll N(char c) {
  if(isdigit(c)) return c-'0';
  return (c-'A')+10;
}
 
ll C(string s,ll n) {
  ll x=0,y=1;
  for(int i=s.size()-1; i>=0; i--) {
    x+=N(s[i])*y;
    y*=n;
  }
  return x;
}
 
int main() {
  ll n;
  string s;
  while(cin >> n >> s && n) {
    vector<P> v=D(n);
    ll m=C(s,n),ans=1LL<<62;
    for(int i=0; i<v.size(); i++) {
      P p=v[i];
      ll z=0,x=p.S;
      while(1) {
        z+=m/x;
        if(x*p.S>m||x*p.S/p.S!=x) break;
        x*=p.S;
      }
      ans=min(ans,z/p.F);
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3044
final_rank: 15

code_snippet: #include <iostream>
#include <string>
#include <map>
#define int long long
#define INF 4e+18
using namespace std;

signed main(){
	int n,tmp = 0;
	string m;
	while(cin >> n >> m,n){
	tmp = 0;
	for(int i = 0;i < m.length();i++){
		if('0' <= m[i] && m[i] <= '9'){
			tmp = tmp * n + m[i] - '0';
		}else {
			tmp = tmp * n + 10 + m[i] - 'A';
		}
	}
	map<int,int> insuu;
	for(int i = 2;i * i <= n;i++){
		while(n % i == 0) {
			insuu[i]++;
			n /= i;
		}
	}
	if(n != 1) insuu[n]++;
	int mi = INF;
	for(auto it = insuu.begin();it != insuu.end();it++){
		int t = tmp,cnt = 0;
		while(t){
			cnt += t / it->first;
			t /= it->first;
		}
		mi = min(mi,cnt / it->second);
	}
	cout << mi << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3084
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef unsigned long long ll;
typedef pair<ll,ll> P;

vector<P> D(ll n) {
  vector<P> v;
  for(ll i=2; i*i<=n; i++) {
    P p=P(0,i);
    while(n%i==0) {
      n/=i;
      p.F++;
    }
    if(p.F) v.push_back(p);
  }
  if(n>1) v.push_back(P(1,n));
  return v;
}

ll N(char c) {
  if(isdigit(c)) return c-'0';
  return (c-'A')+10;
}

ll C(string s,ll n) {
  ll x=0,y=1;
  for(int i=s.size()-1; i>=0; i--) {
    x+=N(s[i])*y;
    y*=n;
  }
  return x;
}

int main() {
  ll n;
  string s;
  while(cin >> n >> s && n) {
    vector<P> v=D(n);
    ll m=C(s,n);
    ll ans=1uLL<<63;
    for(int i=0; i<v.size(); i++) {
      P p=v[i];
      ll z=0,x=p.S;
      while(1) {
        z+=m/x;
        if(x*p.S>m||x*p.S/p.S!=x) break;
        x*=p.S;
      }
      ans=min(ans,z/p.F);
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3224
final_rank: 20

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(long long i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef unsigned long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-2;
const ll inf=1e19;
int main(){
	ll n;
	string s;
	while(cin>>n>>s,n){
		ll m=strtoll(&s[0],NULL,n);
		vi a,b;
		loop(i,2,n+1){
			if(n%i)continue;
			a.pb(i);
			b.pb(0);
			while(n%i==0)b[b.size()-1]++,n/=i;
		}
		ll out=inf;
		rep(i,a.size()){
			ll tmp=m;
			ll t=0;
			while(tmp){
				tmp/=a[i];
				t+=tmp;
			}
			out=min(out,t/b[i]);
		}
		cout<<out<<endl;
	}
}
accuracy: 1
time_ms: 40
memory_kb: 1220
final_rank: 11

code_snippet: #include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int N; string M;
int p[50]; long long power[20];
inline long long div(long long k, int m) {
	long long sum = 0;
	while (k >= 1) {
		k /= m; sum += k;
	}
	return sum;
}
int main() {
	while (true) {
		cin >> N >> M; power[0] = 1;
		for (int i = 0; i < 50; i++)p[i] = 0;
		for (int i = 1; i < M.size(); i++)power[i] = power[i - 1] * N;
		if (N == 0)break;
		long long C = 0;
		for (int i = 0; i < M.size(); i++) {
			int V = (char)(M[i] - '0'); if (M[i] >= 'A')V = (char)(M[i] - 'A') + 10;
			C += power[M.size() - 1 - i] * V;
		}
		int L = N;
		for (int i = 2; i <= L; i++) {
			while (L%i == 0) {
				L /= i; p[i]++;
			}
		}
		long long minx = 1LL << 62;
		for (int i = 2; i <= N; i++) {
			if (p[i] == 0)continue;
			minx = min(minx, div(C, i) / p[i]);
		}
		cout << minx << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3012
final_rank: 14

code_snippet: #include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int N; string M;
int p[50]; long long power[20];
long long div(long long k, int m) {
	long long sum = 0;
	while (k >= 1) {
		k /= m; sum += k;
	}
	return sum;
}
int main() {
	while (true) {
		cin >> N >> M; power[0] = 1;
		for (int i = 0; i < 50; i++)p[i] = 0;
		for (int i = 1; i < M.size(); i++)power[i] = power[i - 1] * N;
		if (N == 0)break;
		long long C = 0;
		for (int i = 0; i < M.size(); i++) {
			int V = (char)(M[i] - '0'); if (M[i] >= 'A')V = (char)(M[i] - 'A') + 10;
			C += power[M.size() - 1 - i] * V;
		}
		int L = N;
		for (int i = 2; i <= L; i++) {
			while (L%i == 0) {
				L /= i; p[i]++;
			}
		}
		long long minx = 1LL << 62;
		for (int i = 2; i <= N; i++) {
			if (p[i] == 0)continue;
			minx = min(minx, div(C, i) / p[i]);
		}
		cout << minx << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1196
final_rank: 2

code_snippet: #include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int d; string s;
int p[11] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
int main() {
	while (cin >> d >> s, d) {
		long long v = 0;
		for (int i = 0; i < s.size(); i++) v = v * d + (s[i] <= '9' ? s[i] - 48 : s[i] - 55);
		long long ret = 9191919191919191919;
		for (int i = 0; i < 11 && p[i] <= d; i++) {
			int e = 0, f = d;
			while (f % p[i] == 0) f /= p[i], e++;
			if (e) {
				long long w = v, x = 0;
				while (w) w /= p[i], x += w;
				ret = min(ret, x / e);
			}
		}
		cout << ret << endl;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3136
final_rank: 17

code_snippet: #include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using namespace std;
using ll=long long;

ll convert(char c){
	if(isdigit(c))
		return c - '0';
	else
		return c -'A'+10;
}

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

int main(void){
	ll n;
	string m;
	while(cin >> n >> m,n){
		ll num[11]={0};		
		ll mten=0LL;

		for(auto &d:m) mten=n*mten+convert(d);
		rep(i,11){
			ll cur=prime[i];
			while(1){
				num[i]+=mten/cur;
				if(cur>mten/prime[i]) break;
				cur*=prime[i];
			}
		}
		ll ans=-1LL;
		rep(i,11){
			ll cur=n,coef=0LL;
			while(cur%prime[i]==0) cur/=prime[i],coef++;  
			if(coef==0) continue;
			if(ans==-1)
				ans=num[i]/coef;
			else
				chmin(ans,num[i]/coef);
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3048
final_rank: 16

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
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;
}
accuracy: 1
time_ms: 40
memory_kb: 1200
final_rank: 9

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#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] = {};
    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;
  }
}
accuracy: 1
time_ms: 30
memory_kb: 1196
final_rank: 2

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#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;
  }
}
accuracy: 1
time_ms: 30
memory_kb: 1196
final_rank: 2

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
char str[30];
int p[30];
int q[30];
int main(){
	int a;
	while(scanf("%d%s",&a,str),a){
		int b=a;
		int sz=0;
		for(int i=0;i<30;i++)q[i]=0;
		for(int i=2;i*i<=b;i++){
			if(b%i==0){
				p[sz++]=i;
				while(b%i==0){
					b/=i;
					q[sz-1]++;
				}
			}
		}
		if(b>1){p[sz++]=b;q[sz-1]=1;}
		long long val=0;
		for(int i=0;str[i];i++){
			val*=a;
			if(str[i]>='A'&&str[i]<='Z')val+=str[i]-'A'+10;
			else val+=str[i]-'0';
		}
		long long ret=(1LL<<62);
		for(int i=0;i<sz;i++){
			long long tmp=0;
			long long now=val;
			while(now){
				tmp+=now/p[i];
				now/=p[i];
			}
			ret=min(ret,tmp/q[i]);
		}
		printf("%lld\n",ret);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1032
final_rank: 1

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }


long long N;
string M;

map<long long, long long> prime_factor(long long n) {
    map<long long, long long> res;
    for (long long i = 2; i*i <= n; ++i) {
        while (n%i == 0) {
            ++res[i];
            n /= i;
        }
    }
    if (n != 1) res[n] = 1;
    return res;
}

int main() {
    while (cin >> N >> M) {
        if (N == 0) break;
        
        long long num = 0;
        for (int i = 0; i < M.size(); ++i) {
            num *= N;
            if (M[i] <= '9') num += M[i] - '0';
            else num += M[i] - 'A' + 10;
        }
        
        //COUT(num);
        
        map<ll, ll> ma = prime_factor(N);
        long long res = 1LL<<62;
        
        //COUT(res);
        
        EACH(it, ma) {
            long long pr = it->first;
            long long ex = it->second;
            
            long long tmp = 0;
            long long tnum = num;
            while (tnum) {
                tmp += tnum / pr;
                tnum /= pr;
            }
            
            //cout << pr << " : " << tmp << endl;
            
            chmin(res, tmp/ex);
        }
        
        cout << res << endl;
    }
    
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1200
final_rank: 9