Codeforces - Sad powers

You’re given Q queries of the form (L, R).

For each query you have to find the number of such x that L ≤ x ≤ R and there exist integer numbers a > 0, p > 1 such that x = ap.

Input
The first line contains the number of queries Q (1 ≤ Q ≤ 105).

The next Q lines contains two integers L, R each (1 ≤ L ≤ R ≤ 1018).

Output
Output Q lines — the answers to the queries.

Example
inputCopy
6
1 4
9 9
5 7
12 29
137 591
1 1000000
outputCopy
2
1
0
3
17
1111
Note
In query one the suitable numbers are 1 and 4.


我们可以想到当数字很大的时候,比如 1e6,大于这个数字的三次方都不可能成为答案。

而且二次方的我们又可以直接开方得到。所以我们记录 2 到 1e6的三次幂即以上所有数字,先存一下,然后二分即可,注意存的时候,不是二次幂才存。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int> v;
int T,l,r,res;
void get(){
	for(int i=2,k;i<=1e6;i++){
		k=i*i*i;	double t=1.0*i*i*i;
		while(t<2e18){
			if((int)sqrt(k)*(int)sqrt(k)<k)	v.push_back(k); 
			k*=i; t*=i;
		}
	}
	sort(v.begin(),v.end());	v.erase(unique(v.begin(),v.end()),v.end());
}
inline int find(int x){return lower_bound(v.begin(),v.end(),x)-v.begin();}
inline void solve(){
	cin>>l>>r;	res=0;	res+=(find(r+1)-find(l));
	int num1=sqrt(l-1),num2=sqrt(r);	res+=(num2-num1);
	printf("%lld\n",res);
}
signed main(){
	get();	cin>>T;
	while(T--)	solve();
	return 0;
}
发布了416 篇原创文章 · 获赞 228 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/103804462
sad