题目:http://codeforces.com/contest/955/problem/C
题意:q次询问,每次给出l,r 问 l~r 有多少数满足存在 a > 0, p > 1 x = ap;
自己做的时候就简单想到把所有满足条件的数放入vector中,然后发现满足条件的数字太多,一脸懵逼,然后放弃抵抗。看了题解才直到这道题目要稍微动一下脑筋
思路:对于1018来说满足条件的数太多,思考一下更高次方,如果p>=3,可以知道满足条件的数字最多也就,106个,满足p=2的数有108个,那么就需要单独判断2次方的情况,首先预处理讲所有3次方以上满足条件的数放入vector中,然后每次询问直接二分查找数目即可,这里要注意,某一些高次方的数也可能是一些数的平方,这里要特别判断不能放入vector中,不然下面单独计算平方数的时候会重复计数,还有就是要去除相同元素。
对于每次询问判断平方数的个数时,不能使用sqrt,会导致精度出错,需要自己用二分判断数目。
#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e18 + 10;
const int N = 1e6 + 10;
const ll mod = 1e9+7;
const int base=131;
const double pi=acos(-1);
map<string, int>ml;
map<ll,ll> mp;
map<int,int> vi;
ll b[N], visrow[N],viscol[N], dep[N],num[N],a[N] ,n, m, k,x,y;
ll ex, ey, cnt, ans, sum, flag;
ll nex[N];
//vector<int> v[N];
vector<ll> v;
map<ll,ll> dp[N];
set<ll> se;
string s,ss;
ll solve(ll x)
{
ll l=0,r=1e9,mid,ans;
while(l<=r)
{
mid=(l+r)>>1;
if(mid*mid<=x)
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
return ans;
}
int main( )
{
ios::sync_with_stdio(false);
cin.tie(0);
for(ll i=2;i<=1e6;i++)
{
ll t=i*i*i;
while(t<=inf)
{
if(solve(t)*solve(t)<t)
{
v.push_back(t);
}
if(t>=inf/i) break;
t*=i;
//show2("t",t)
}
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
int q;
cin>>q;
ll l,r;
while(q--)
{
cin>>l>>r;
ans=0;
ans+=upper_bound(v.begin(),v.end(),r)-lower_bound(v.begin(),v.end(),l);
ans+=solve(r)-solve(l-1);
cout<<ans<<endl;
}
}