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

题目链接:https://codeforces.com/contest/1114/problem/C

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

题解:等价于求最大的k使得n!%(b^k) == 0

先把b分解质因数,

求n!中质因子p的次方结论:cnt = n/p + n/(p^2) + n/(p^3) + ...

对于b的所有质因子,在n!中最小的次数即为答案

#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;

const int maxn = 4e6+5;
ll pri[maxn], m, num[maxn];

void getprime(ll x){
    m = 0;
    for(ll i = 2; i <= sqrt(x); i++){
        if(x % i == 0){
            pri[m]= i;
            ll cnt = 0;
            while(x % i == 0){
                x /= i;
                cnt++;
            }
            num[m++] = cnt;
        }
    }
    if(x != 1) pri[m] = x, num[m++] = 1;
}

int main(){
    ll n, b;
    cin >> n >> b;
    getprime(b);

    ll ans = 9e18, tmp, t, x;
    for(ll i = 0; i < m; i++){
        x = n;
        t = pri[i];
        tmp = 0;
        while(x){
            tmp += x / t;
            x /= t;
        }
        ans = min(ans, tmp/num[i]);
    }
    cout << ans << endl;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值