码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5297 Y sequence

时间:2015-07-24 20:55:55      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:


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
2 10 2 10 3
 

Sample Output
13 14
 

Author
FZUACM
 

Source
 

#include <bits/stdc++.h>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
typedef 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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5297 Y sequence

标签:

原文地址:http://blog.csdn.net/oilover/article/details/47045227

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!