http://codeforces.com/problemset/problem/1114/C
Split the b prime factors as b=(a1^p1)*(a2^p2)*...*(ak^pk)
To make up a trailing 0, you need p1 a1 p2 a2...pk ak and then see how much n can provide and take the minimum.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=0x3f3f3f3f3f3f3f3f;
int main()
{
ll n,b,i,lim,sum,val,tmp,ans;
scanf("%lld%lld",&n,&b);
ans=N;
for(i=2;i*i<=b;i++){
if(b%i==0){
lim=0;
while(b%i==0){
b/=i;
lim++;
}
sum=0,val=1,tmp=i;
while(1){
if(n/val<tmp) break;
val*=tmp;
sum+=n/val;
}
ans=min(ans,sum/lim);
}
}
if(b!=1){
sum=0,val=1,tmp=b;
while(1){
if(n/val<tmp) break;
val*=tmp;
sum+=n/val;
}
ans=min(ans,sum);
}
printf("%lld\n",ans);
return 0;
}
//1000000000000000000 97