2018CCPC女生赛 C 二分+取整Log

本文介绍了一种使用二分查找法解决特定形式数学不等式的方法,针对给定的a,b,K参数,求解n的最大值使不等式成立。通过自定义乘法函数和对数计算过程,有效避免了数值运算中的溢出和精度问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接


题意:
给定三个整数 a , b , K a,b,K a,b,K,求使得 n a ⌈ l o g 2 n ⌉ b &lt; = K n^a {\lceil log_2n\rceil}^b &lt;= K nalog2nb<=K n n n的最大值。


思路:
因为函数单调,显然可以直接二分 n n n去验证。

此题有两个需要注意的地方:
一是对于log以2为底的函数取整,不能直接使用log函数来求,会带来精度的误差。需要模拟计算过程对2重复做除法。
二是 k k k的范围最大为 1 e 18 1e18 1e18,使用乘法的过程中可能会产生溢出,故需要自定义乘法函数,用来避免计算的溢出,具体实现可参考代码。

此题得解。


代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
typedef long long ll;
using namespace std;

ll a,b,K;

ll Mul(ll a,ll b){
    if(!a || !b) return 0;
    if(a>(K+5)/b) return K+5;
    a *= b;
    return a>(K+5)?K+5:a;
}

ll GetLog(ll x){
    ll y = x;
    while(y%2 == 0) y/=2;
    ll res = 0;
    while(x>1) x/=2,res++;
    if(y>1) res++;
    return res;
}

ll Pow(ll n,ll m){
    ll res = 1;
    while(m--){
        res = Mul(res,n);
    }
    return res;
}

bool check(ll n){
    if(Mul(Pow(n,a),Pow(GetLog(n),b)) <= K) return true;
    return false;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%I64d%I64d%I64d",&a,&b,&K);
        ll l = 1,r = K,ans = 0;
        while(l<=r){
            ll mid = (l+r)>>1;
            if(check(mid)){
                ans = mid;
                l = mid + 1;
            }
            else r = mid - 1;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值