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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值