题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5297
Description
Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar calls the sequence that formed by the rest integers“Y sequence”.When r=3,The first few items of it are:
2,3,5,6,7,10......
Given positive integers n and r,you should output Y(n)(the n-th number of Y sequence.It is obvious that Y(1)=2 whatever r is).
Input
The first line of the input contains a single number T:the number of test cases.
Then T cases follow, each contains two positive integer n and r described above.
n<=2*10^18,2<=r<=62,T<=30000.
Output
For each case,output Y(n).
Sample Input
2
10 2
10 3
Sample Output
13
14
题意:给出序列1,2,3...MAX; 去掉其中可以表示为a^b(2<=b<=r)的数,求出新序列中第n项是什么。
题解:首先,知道x^(1/b)表示x范围内有多少个a^b次方的数,容斥幂就可以求出r内的解,
由于MAX很大,二分会超时,用迭代来做,令ans=n,求出上限为ans的序列长度len,显然,len<=n
上限为ans的序列中有len个数,然后将上限加n-len,逐渐逼近于答案。
//#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define bug cout<<"bug"<<endl
int r;
int prime[107],np;
vector<int> mi;
void get_prime()
{
np=0;
for(int i=2; i<=100; ++i)
{
int f=1;
for(int j=0; j<np; ++j)
if(i%prime[j]==0)f=0;
if(f)prime[np++]=-i;
}
}
void getr()
{
mi.clear();
for(int i=0; -prime[i]<=r; ++i)
{
int all=mi.size();
for(int j=0; j<all; ++j)
if(abs(mi[j]*prime[i])<=62)
mi.push_back(mi[j]*prime[i]);
mi.push_back(prime[i]);
}
}
long long cal(long long x)
{
long long ans=0;
int all=mi.size();
for(int i=0; i<all; ++i)
{
int b=mi[i];
long long temp=pow(double(x+0.5),1.0/abs(b))-1;
if(b<0)ans+=temp;
else ans-=temp;
}
return x-ans-1;
}
int main()
{
int T;
long long n;
get_prime();
scanf("%d",&T);
while(T--)
{
scanf("%I64d%d",&n,&r);
getr();
long long temp=cal(n),ans=n;
while(temp<n)
{
ans+=n-temp;
temp=cal(ans);
}
printf("%I64d\n",ans);
}
return 0;
}
/*
2
10 2
10 3
*/