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


当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值