题目链接:https://codeforces.com/contest/1114/problem/C
题意:求n!在b进制下的末尾0的个数
题解:等价于求最大的k使得n!%(b^k) == 0
先把b分解质因数,
求n!中质因子p的次方结论:cnt = n/p + n/(p^2) + n/(p^3) + ...
对于b的所有质因子,在n!中最小的次数即为答案
#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int maxn = 4e6+5;
ll pri[maxn], m, num[maxn];
void getprime(ll x){
m = 0;
for(ll i = 2; i <= sqrt(x); i++){
if(x % i == 0){
pri[m]= i;
ll cnt = 0;
while(x % i == 0){
x /= i;
cnt++;
}
num[m++] = cnt;
}
}
if(x != 1) pri[m] = x, num[m++] = 1;
}
int main(){
ll n, b;
cin >> n >> b;
getprime(b);
ll ans = 9e18, tmp, t, x;
for(ll i = 0; i < m; i++){
x = n;
t = pri[i];
tmp = 0;
while(x){
tmp += x / t;
x /= t;
}
ans = min(ans, tmp/num[i]);
}
cout << ans << endl;
}