n!在m进制下末尾0的个数
输入n,m求n!在m进制下末尾0的个数。
n, m<1e18
#include<bits/stdc++.h> #define LL long long using namespace std; int prime[]={0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; LL ind[105], cnt[105]; LL getcnt(LL x, LL n){//计算1-n中所有质因子x的总和个数 LL res=0; while(n){ res+=n/x; n/=x; } return res; } int main(){ LL n, m; scanf("%lld%lld", &n, &m); for(int i=1; i<=25; i++){ LL mm=m; while(mm%prime[i]==0){ ind[prime[i]]++; mm/=prime[i]; } } for(int i=1; i<=25; i++){ if(ind[prime[i]]){ cnt[prime[i]]=getcnt(prime[i], n); } } LL ans=1e18+1; for(int i=1; i<=25; i++){ if(ind[prime[i]]){ ans=min(ans, cnt[prime[i]]/ind[prime[i]]); } } cout<<ans<<endl; return 0; }