【Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs?)】 分解质因数


C. Trailing Loves (or L’oeufs?)

题意

n ! n! 在b进制下末尾有多少个 0 0
1 10 1 0 18 , 2 b 1 0 12 1 \leq 10 \leq 10^{18},2 \leq b \leq 10^{12}

做法

这道题我们首先考虑 10 10 进制,那就是看 n ! n! 能够整除 5 5 多少次和整除 2 2 多少次。取个 m i n min 即是答案,这道题做法类似,我们只需要先对 b b 进行分解质因数,对每个质因数统计在 b b 中出现次数 c n t cnt 和在 n n! 中出现次数 m m ,最后对所有的 m / c n t m/cnt m i n 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;
}

猜你喜欢

转载自blog.csdn.net/qq_38891827/article/details/88072414
今日推荐