2015 Multi-University Training Contest 1 y sequence

Y sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 667    Accepted Submission(s): 147


Problem 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
 

 

Author
FZUACM
 

 

Source
 
解题:传说中的容斥原理。
 
先计算1到n间有多少个数被删除了,那么我们就还需要m=n+删除的数目,看看又又多少个删除了,看看剩下的是不是刚好n个,否则补上n - 剩下的个数,继续搞
 
处理1的时候,先把1都不算,最后才算,奇数个素因子的乘积那么要加上,偶数个素因子的乘积要减去
 
参考了 这位博主的写法
 
可以由$\sqrt[i]{a}$得出范围内指数是i的元素的个数  
2015 Multi-University Training Contest 1 y sequence
 1 #include <bits/stdc++.h>

 2 using namespace std;

 3 typedef long long LL;

 4 const int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67};

 5 vector<int>d;

 6 LL n,r;

 7 void init() {

 8     d.clear();

 9     for(int i = 0; p[i] <= r; ++i) {

10         for(int j = d.size()-1; j >= 0; --j)

11             if(abs(d[j]*p[i]) <= 63) d.push_back(-d[j]*p[i]);

12         d.push_back(p[i]);

13     }

14 }

15 LL calc(LL x){

16     if(x == 1) return 0;

17     LL ret = x;

18     for(int i = d.size()-1; i >= 0; --i){

19         LL tmp = pow(x+0.5,1.0/abs(d[i])) - 1;

20         if(d[i] < 0) ret += tmp;

21         else ret -= tmp;

22     }

23     return ret-1;

24 }

25 LL solve(){

26     init();

27     LL ret = n;

28     while(true){

29         LL tmp = calc(ret);

30         if(tmp == n) break;

31         ret += n - tmp;

32     }

33     return ret;

34 }

35 int main() {

36     ios::sync_with_stdio(false);

37     int kase;

38     cin>>kase;

39     while(kase--){

40         cin>>n>>r;

41         cout<<solve()<<endl;

42     }

43     return 0;

44 }
View Code

 

你可能感兴趣的:(sequence)