【CF#538div2:C】Trailing Loves (or L'oeufs?)(质因数分解+分析)

版权声明:转载请注明出处哦~ https://blog.csdn.net/Cassie_zkq/article/details/89340665

题目地址:https://codeforces.com/contest/1114/problem/C

题意


给出十进制的数n,和要转换的进制b,求n!转化成b进制之后末尾有多少个0

解题思路


就是求最大的k,使  \frac{n!}{b^{k}}=0

对b质因子分解:b=p_1{}^{a1}*p_2{}^{a2}*p_3{}^{a3}*...*p_n{}^{an} ,记录p_{i}对应的个数a_{i}(用map存对应关系,vector存p_{i}

对n!质因子分解:n!=p_1{}^{b1}*p_2{}^{b2}*p_3{}^{b3}*...*p_n{}^{bn}*x1*x2..*xn(后面的x1...xn在求结果的时候不用考虑)

ans=min(\frac{a_{i}}{b_{i}}),表示n!最多可以整除b的ans次方,即结果

ac代码


#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <sstream>
#define  maxn 1005
typedef long long ll;
using namespace std;
ll n,b,ans=2e+18;
vector<ll> p;
map<ll,ll> m;//质因子对应的个数
void solve_b() //对b质因子分解
{
    for(ll i=2;i*i<=b;i++)
    {
        if(b%i==0)
        {
            p.push_back(i);
            while(b%i==0)
            {
                m[i]++;
                b/=i;
            }
        }
    }
    if(b!=1)
    {
        m[b]++;
        p.push_back(b);
    }
}
ll numn(ll x)//返回n中质因子的个数
{
    ll num=0,a=n;
    while(a/x)
    {
        num+=a/x;
        a/=x;
    }
    return num;
}
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    scanf("%lld %lld",&n,&b);
    solve_b();
    for(int i=0;i<p.size();i++)
        ans=min(ans,numn(p[i])/m[p[i]]);
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Cassie_zkq/article/details/89340665