CodeForces - 1114C n的阶乘用m进制表示末尾零的个数

Trailing Loves (or L’oeufs?)

The number “zero” is called “love” (or “l’oeuf” to be precise, literally means “egg” in French), for example when denoting the zero score in a game of tennis.
Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.

However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.

Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n! (factorial of n).

Input
The only line of the input contains two integers n and b (1≤n≤1018, 2≤b≤1012 ).

Output
Print an only integer — the number of trailing zero digits in the b-ary representation of n!

题意:
求n!在b进制下末尾0的个数;

分析:
先抛开n!的计算,先看在b进制下,末尾零的个数是否存在一定的性质或者规律
举个栗子:
在十进制下1200可以表示为12*102
在二进制下1100可以表示为23+22

通过上面这两个例子,我们可以得出,最后的零的个数是和该进制下最小的指数有关的,也就是说最小的指数是k的话,那么该进制下的数值就存在k个0;

那么剩下的问题就是怎么处理n的阶乘呢?
在这里插入图片描述
在任意进制下也是 即n!在b进制下 n!=a*bx 那么末尾0的个数就是 x

若b能分解出质因数 b1 b2 b3 …

那么 abx = a*(b1x1 * b2x2 * b3x3 … )x = a*(b1(x1*x) * b2(x2*x) * b3(x3*x) … )

对于特定的B 通过getcnt()可以求X 对于b1可求得X1 那么x=X1/x1

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int N = 1e5 + 10;
const int mod = 1e9+7;
const ll INF =0x3f3f3f3f3f3f3f3f;
ll n,k;
ll prime[N],cnt,sum[N];
ll get_sum(ll x){
    ll ans=0;
    ll m=n;
    while(m){
        ans+=m/x;
        m/=x;
    }
    return ans;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    scanf("%lld%lld",&n,&k);
    ll ans=INF;
    for(ll i=2;i*i<=k;i++){
        ll tot=0;
        if(k%i==0){
            while(k%i==0)
                tot++,k/=i;
        ans=min(ans,get_sum(i)/tot);
//        cout<<ans<<endl;
        }
    }
    if(k>1)
    ans=min(ans,get_sum(k)),
//    cout<<ans<<endl;
    printf("%lld\n",ans);
    return 0;
}

发布了229 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/101077456
今日推荐