Codeforces 955C Sad powers 基础数论+二分

传送门:Sad powers

分析: 先固定指数p,x^p <= 1e18 可以推导出 x <= 10^(18/p) [取对数推导]

若 p == 2, 则有 10^9 那么多的数字 满足条件

若 p >= 3, 则最多有 10^6 的数字满足 条件

先预处理,把所有不是 完全平方数且满足 x^p<=1e18(p>=3) 的x都加入到vector, 排序去重

对于每次查询 输出  sqrt(R) - sqrt(L-1) + upper_bound(R) - lower_bound(L)

对于快速查询x的平方根,二分查询

总的时间复杂度为 (1e6 + Qlog1e18)


代码如下:

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long LL;

LL root(LL x) {
    LL left = 0, right = 1e9;
    LL mid ,ans;
    while (left<=right) {
        mid = (left+right)>>1;
        if (mid*mid<=x) {ans = mid; left = mid+1;}
        else right = mid-1;
    }
    return ans;
}

vector<LL>g;
void init(){
    g.clear();
    for (LL i=2; i<=1e6; i++) {
        double t = 1.0*i*i*i;
        LL  s = i*i*i;
        while (t<2e18) {
            LL root_s = root(s);
            if (root_s*root_s<s) g.push_back(s);
            t *= i;
            s *= i;
        }
    }
    sort(g.begin(),g.end());
    int sz = unique(g.begin(),g.end())-g.begin();
}

int main(){
    init();
    int q;
    scanf("%d",&q);

    LL l,r;
    while (q--) {
       scanf("%I64d %I64d",&l,&r);
       int ans1 = upper_bound(g.begin(),g.end(),r) - lower_bound(g.begin(),g.end(),l);
       int ans2 = root(r) - root(l-1);
       printf("%d\n",ans1+ans2);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值