求N!的B进制下末尾有几个零
质因数分解
B = ∏ p i k i B = \prod p_i^{k_i} B=∏piki
接下来即是求解N!中pi的指数处以ki取最小值
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 +100;
const int MAX_M = 10010;
typedef long long LL;
#define sci(num) scanf("%d",&num)
LL facs[MAX_N];
LL cals[MAX_N];
LL cal(LL N,LL num) {
LL cnt= 0 ;
while (N) N /= num,cnt += N;
return cnt;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
LL N,B;
facs[0] = 0;
cin >> N >> B;
LL t= B;
for (LL i= 2;i * i <=t;i++) {
if (t % i == 0) {
int cnt1= 0;
while (t % i == 0) {
t /= i;
cnt1++;
}
facs[++facs[0]] = i;
cals[++cals[0]] = cnt1;
}
}
if (t > 1) {
facs[++facs[0]] = t;
cals[++cals[0]] = 1;
}
LL ans = N;
for (int i = 1;i <= facs[0];i++) {
ans = min(ans,cal(N,facs[i]) / cals[i]);
}
cout << ans << endl;
return 0;
}