CodeForces 955C :Sad powers 打表+二分

传送门

题目描述

给你q个询问(1 <= q <=1e5),每次询问l到r这个区间内(1 <= l <= r <= 1e18),满足x=a^p的x的数量(a>0,p>1)。

输入格式:

第一行输入一个整数q。

第二行到第q+1行每行输入两个整数l,r。

输出格式:

一共q行,每行一个数,表示l到r之间有多少满足条件的数。

分析

首先如果我们需要计算平方数的话,直接通过sqrt计算即可,那么p大于2的情况如何计算呢
提前把1e18之内的符合条件的数字打出来,然后二分查找位置即可

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const ll n = 1e18;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){
    
    char c=getchar();T x=0,f=1;while(!isdigit(c)){
    
    if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
    
    x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
vector<ll> nums;
int q;

ll cal(ll x){
    
    
    ll ans = upper_bound(nums.begin(), nums.end(),x) - nums.begin();
    // debug(x);
    // debug(ans);
    if(x >= nums[nums.size() - 1]) ans = nums.size();
    ans += ((ll)sqrt(x));
    return ans;
}

int main(){
    
    
    for(ll i = 2;i * i * i <= n;i++){
    
    
        ll s = i * i;
        while(s <= n / i){
    
    
            s *= i;
            ll t = sqrt(s);
            if(t * t == s) continue;
            nums.push_back(s);
        }
    }
    sort(nums.begin(), nums.end());
    nums.erase(unique(nums.begin(), nums.end()),nums.end());
    read(q);
    while(q--){
    
    
        ll l,r;
        read(l),read(r);
        dl(cal(r) - cal(l - 1));
    }
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/113442464
sad
今日推荐