HDU 5297 Y sequence

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

2015 Multi-University Training Contest 1

#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;
}

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

时间: 2025-01-01 17:07:16

HDU 5297 Y sequence的相关文章

hdu 5297 Y sequence(容斥)

题目链接:hdu 5297 Y sequence 考虑62以内的指数,x为奇数个质数因子,就减掉,偶数个加上.计算x为指数的不满足数直接pow(n,1/x)即可. #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <algorithm> using namespace std; type

[2015hdu多校联赛补题]hdu 5297 Y sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5297 题意:给你一个所有正整数的序列,然后去掉满足x^(2~r)的所有数(x为所有正整数,r>=2题目给出),问你现在这个序列的第n个数是什么 解:首先想到写一个函数func(y),它可以计算出给定数字y是序列中第几个数,这样我们大概可以二分答案~(事实上会TLE,得用迭代法,当然迭代的话也是用这个函数) 那么如何实现func: 首先想去掉满足x^2的所有数,我们可以用pow(y, 1/2)计算出y

HDU 5297 Y sequence 容斥/迭代

Y sequence 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 ca

HDU 5297 Y sequence Y数列

题意:给定正整数n和r.定义Y数列为从正整数序列中删除全部能表示成a^b(2 ≤ b ≤ r)的数后的数列,求Y数列的第n个数是多少. 比如n = 10. r = 3,则Y数列为2 3 5 6 7 10 11 12 13 14,第10个数是14. 非常有趣的一道数论题.题目给出的范围是long long范围的. 所以显然不用去枚举每一个数了,更不用说推断每一个数是不是某个数的某次方.那么这个题怎么下手呢.首先我们能够非常直观的想到,被删去的数显然是非常分散的.由于能表示成某个数的幂这种形式的数非

[多校2015.01.1010 容斥+迭代] hdu 5297 Y sequence

题意: 给你一个n和一个r,求Y序列的第N项是多少. 所谓的Y序列就是,从1开始,去掉能表示成a^b(2<=b<=r)的数,所构成的序列 例如r=2 序列就是:2,3,5,6,7,8,10,11,12,13,14,15,17.... 思路: 我们应该能想到需要一个函数fun(x) 求的是1~x内在Y序列里的数有多少个 这个其实不难,我们可以运用容斥原理,通过63以内的素数进行计算,并且最多做三遍,因为2*3*5*7>63 然后就是一个很神奇的方法了,这个方法特别的秒 就是迭代的方法. 假

HDU 5297(Y sequence-Mobius函数容斥+迭代)

Y sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1192    Accepted Submission(s): 265 Problem Description Yellowstar likes integers so much that he listed all positive integers in asce

HDOJ 5297 Y sequence 容斥原理

Y sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1174    Accepted Submission(s): 260 Problem Description Yellowstar likes integers so much that he listed all positive integers in asce

hdu 4441 Queue Sequence(splay)

题目链接:hdu 4441 Queue Sequence 这题看了题解写的,题解传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 #define ls l,m,rt<<1 4 #define rs m+1,r,rt<<1|1 5 using namespace std; 6 typedef long long ll; 7 8 const int N=1e6+7; 9 i

hdu 5306 Gorgeous Sequence(区间最值更新+求和)

题目链接:hdu 5306 Gorgeous Sequence 题意: 给你一个序列,有三种操作. 0 x y t:将[x,y]的数取min(a[i],t) 1 x y:求[x,y]的最大值 2 x y:求[x,y]的区间和 题解: 吉老师的课件题:传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 #define ls l,m,rt<<1 4 #define rs m+1,r,rt