【Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs?)】 分解质因数
作者:互联网
C. Trailing Loves (or L’oeufs?)
题意
求n!在b进制下末尾有多少个0
1≤10≤1018,2≤b≤1012
做法
这道题我们首先考虑10进制,那就是看n!能够整除5多少次和整除2多少次。取个min即是答案,这道题做法类似,我们只需要先对b进行分解质因数,对每个质因数统计在b中出现次数cnt和在n!中出现次数m,最后对所有的m/cnt取min即可
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
ll solve(ll a,ll b)
{
if(a==0) return 0;
return a/b+solve(a/b,b);
}
int main()
{
ll a,b;
scanf("%lld%lld",&a,&b);
ll ans=LL_INF;
for(ll i=2;i*i<=b;i++)
{
if(b%i==0)
{
int cnt=0;
while(b%i==0)
{
cnt++;
b/=i;
}
ans=min(ans,solve(a,i)/cnt);
}
}
if(b>1) ans=min(ans,solve(a,b));
printf("%lld\n",ans);
return 0;
}
标签:10,return,ll,Codeforces,leq,oeufs,Loves,ans,include 来源: https://blog.csdn.net/qq_38891827/article/details/88072414