阶乘
description
求\(n\)的阶乘转\(k\)进制后末尾\(0\)的个数
solution
容易发现,十进制数\(x\)转成\(k\)进制后,末尾\(0\)的个数即\(x\)因子中\(k\)的最高次幂的指数.于是预处理质因子然后打擂台即可.数据会爆longlong,特判一下下即可.
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define R register
#define next kdjadskfj
#define debug puts("mlg")
#define mod K
#define Mod(x) ((x%mod+mod)%mod)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
inline ll read();
inline void write(ll x);
inline void writeln(ll x);
inline void writesp(ll x);
ll T;
ll n,base,ans;
ll son[3000000],p[3000000],h;
ll sum,prime[3000000],v[3000000];
inline void Get(){
for(R ll i=2;i<=3000000;i++){
if(!v[i]) prime[++sum]=i,v[i]=i;
for(R ll j=1;j<=sum&&prime[j]*i<=3000000;j++) v[i*prime[j]]=prime[j];
}
}
inline ll Get10(ll x){
ll sum=0;
for(R ll i=x;i;i/=10)++sum;
return sum;
}
inline bool check(ll x,ll y){
if(Get10(x)+Get10(y)-1>18) return false;return true;
}
int main(){
T=read();
Get();
while(T--){
n=read();base=read();
h=0;
for(R ll i=1;prime[i]*prime[i]<=base;i++){
if(base%prime[i]==0){
son[++h]=prime[i];
p[h]=0;
while(base%prime[i]==0) ++p[h],base=base/prime[i];
}
}
if(base>1) son[++h]=base,p[h]=1;
ans=((ull)1<<63)-1;
for(R ll i=1;i<=h;i++){
ll sum=0;
for(R ll j=son[i];j<=n;j*=son[i]){
sum+=n/j;
if(!check(son[i],j)) break;
}
ans=min(ans,sum/p[i]);
}
writeln(ans);
}
}
inline ll read(){ll x=0,t=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') t=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*t;}
inline void write(ll x){if(x<0){putchar('-');x=-x;}if(x<=9){putchar(x+'0');return;}write(x/10);putchar(x%10+'0');}
inline void writesp(ll x){write(x);putchar(' ');}
inline void writeln(ll x){write(x);putchar('\n');}