Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs?)

本文详细解析了CodeForces比赛中的1114C题目,介绍了如何通过分解质因数和利用筛素数原理来解决该问题。通过记录质因数及其个数,并计算1至n中对应质因数的个数,最终求得最小比值。

地址:http://codeforces.com/contest/1114/problem/C

刚开始想错了,每次一有思路就着急敲代码,每次敲完之后输样例发现走不通了,应该对应每个样例之后都解释的通,心中敲定做法之后,然后再敲;
思路:
对b分解质因数之后,记录其质因数及其个数a;
然后再求其1~n中对应质因数的个数b(利用筛素数的原理,先找出公因子为一个b的个数(n / b),再求公因子为两个b的(n / b / b));
在求其b[i] / a[i]的最小值。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const LL linf = 1e18 + 5;

LL zhuan(LL n,LL b)
{
    //筛素数的思想
    LL sum = 0;
    while(n >= b){
        sum += (n / b);
        n = n / b;
    }
    return sum;
}

map<LL,LL>mk;
pair<LL,LL> num[105];
LL ptr[105];

int main()
{
    LL n,b;
    cin >> n >> b;

    LL m = b;
    for(LL i = 2;i * i <= m;i++){
        while(m != i){
            if(m % i == 0){
                mk[i]++;
                m = m / i;
            }
            else
                break;
        }
    }
    if(m != 1){
        mk[m]++;
    }

    int len = mk.size();
    int cnt = 0;
    for(map<LL,LL>::iterator it = mk.begin();it != mk.end();++it){
        num[cnt++] = mp(it -> fi,it -> se);
    }
    for(int i = 0;i < cnt;++i){
        ptr[i] = zhuan(n,num[i].fi);
    }

    LL MIN = linf;
    for(int i = 0;i < cnt;++i){
        MIN = min(MIN,ptr[i] / num[i].se);
    }
    cout << MIN << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值