HDU 5297 Y sequence

来源:互联网 发布:淘宝客贷怎么才能贷款 编辑:程序博客网 时间:2024/05/17 06:59


Y sequence

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


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
210 210 3
 

Sample Output
1314
 

Author
FZUACM
 

Source
2015 Multi-University Training Contest 1
 

#include <bits/stdc++.h>using namespace std;#define prt(k) cerr<<#k" = "<<k<<endltypedef long long ll;const int N = 63;int p[N];ll n; int r;int sign[N];vector<int> rc;vector<int> mi;void get_rc(){        rc.clear();        for (int x : mi) {                int n = rc.size();                if (abs(x) > r) break;                for (int j=0;j<n;j++)                        if (abs(x*rc[j]) <= 62)                        rc.push_back(x*rc[j]);                rc.push_back(x);        }}ll f(ll n, int r){        if (n==1) return 0;        ll ans  = n - 1;        for (ll x:rc) {                ll t = pow(n+0.5, 1.0/abs(x) ) - 1;                if (x < 0)                        ans -= t;                else                        ans += t;        }        return ans ;}int main(){        for (int i=1;i<N;i++) p[i] = i;        for (int i=2;i<N;i++) if (p[i]==i) {                for (int j=i+i; j<N; j+=i) p[j] = i;                mi.push_back(-i);        }        int re, ca=1;        scanf("%d", &re);        while (re--) {                scanf("%I64d%d", &n, &r);                get_rc();                ll x = n ;                while (1) {                        ll t = f(x, r);                        if (t >= n) break;                        x += n - t;                }                printf("%I64d\n", x);        }        return 0;}



2 0
原创粉丝点击