C. Trailing Loves (or L'oeufs?) (质因数分解)

C. Trailing Loves (or L'oeufs?) 

题目传送门

题意:

  求n!在b进制下末尾有多少个0?


思路:

  类比与5!在10进制下末尾0的个数是看2和5的个数,那么

原题就是看b进行质因数分解后,每个因数个数的最小值

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 1000005
ll pri[N];
ll cnt[N];
ll tot;
void getpri(ll x)
{
    memset(cnt,0,sizeof(cnt));
    memset(pri,0,sizeof(pri));
    for(ll i=2;i*i<=x;i++)
    {
        while(x%i==0)
        {
            pri[tot]=i;
            cnt[tot]++;
            x/=i;
        }
        if(cnt[tot]) tot++;
    }
    if(x>1) pri[tot]=x,cnt[tot++]++;
}
ll solve(ll x,ll p)
{
    ll res=0;
    while(x)
    {
        res+=x/p;
        x/=p;
    }
    return res;
}
int main()
{
    ll n,b;
    while(~scanf("%lld %lld",&n,&b)){
            tot=0;
        getpri(b);
        ll maxn=1e18;
        /*for(int i=0;i<tot;i++)
            cout<<pri[i]<<" "<<cnt[i]<<endl;*/
        for(ll i=0;i<tot;i++)
        {
            maxn=min(maxn,solve(n,pri[i])/cnt[i]);
        }
        printf("%lld\n",maxn);
    }

    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/zhgyki/p/10367492.html
今日推荐