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

Recommend

We have carefully selected several similar problems for you:  5390 5389 5388 5387 5386

Mobius函数+迭代

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXT (30000+10)
#define MAXR (63)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

const long long prime[] = {2,3,5,7,11,13,17,19,23,29,
           31,37,41,43,47,53,59,61,67,
           71,73,79,83,89,97};
class Math
{
public:
	ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);}
	ll abs(ll x){if (x>=0) return x;return -x;}
	ll exgcd(ll a,ll b,ll &x, ll &y)
	{
	    if (!b) {x=1,y=0;return a;}
	    ll g=exgcd(b,a%b,x,y);
	    ll t=x;x=y;y=t-a/b*y;
	    return g;
	}  

	ll pow2(ll a,int b,ll p)  //a^b mod p
	{
	    if (b==0) return 1;
	    if (b==1) return a;
	    ll c=pow2(a,b/2,p);
	    c=c*c%p;
	    if (b&1) c=c*a%p;
	    return c;
	}
	ll Modp(ll a,ll b,ll p)  //a*x=b (mod p)
	{
	    ll x,y;
	    ll g=exgcd(a,p,x,y),d;
	    if (b%g) {return -1;}
	    d=b/g;x*=d,y*=d;
	    x=(x+abs(x)/p*p+p)%p;
	    return x;
	}  

	ll mobius(ll x)
	{

	}
}S;

ll n;
int r;

int num[600000],Nnum,mobnum[600000],rNnum[600000];
void get() {
	Nnum=0;
	for(int i=0;prime[i]<=MAXR;i++) {
		int m=Nnum;
		For(j,m) {
			if (prime[i]*num[j]<=MAXR) {
				num[++Nnum]=prime[i]*num[j];
				mobnum[Nnum]=-mobnum[j];
			}
		}
		num[++Nnum] = prime[i];
		mobnum[Nnum]=-1;
		rNnum[i]=Nnum;
	}
}

ll calc(ll x) {
	ll ans=0;

	int rN=0;
	while (prime[rN+1]<=r) ++rN;

	For(i,rNnum[rN])
	{
		ans+=-mobnum[i]*(ll)(pow(x+0.1,1.0/num[i])-1);
	}

	return x-ans-1;
}

ll Y(ll n,int r)
{
	ll ans=n;
	while(1) {
		ll tmp=calc(ans);
		if (tmp==n) break;
		ans+=n-tmp;
	}
	return ans;
}

int main()
{
//	freopen("J.in","r",stdin);
//	freopen(".out","w",stdout);

	get();
//	For(i,Nnum) cout<<num[i]<<' '<<mobnum[i]<<endl;

	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%lld%d",&n,&r);
		printf("%lld\n",Y(n,r));
	}

	return 0;
}

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

时间: 2024-10-29 06:38:17

HDU 5297(Y sequence-Mobius函数容斥+迭代)的相关文章

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

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

[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 1695 GCD 欧拉函数+容斥

题意:给定a,b,c,d,k x属于[1 , c],y属于[1 , d],求满足gcd(x,y)=k的对数.其中<x,y>和<y,x>算相同. 思路:不妨设c<d,x<=y.问题可以转化为x属于[1,c / k ],y属于[1,d/k ],x和y互质的对数. 那么假如y<=c/k,那么对数就是y从1到c/k欧拉函数的和.如果y>c/k,就只能从[ c/k+1 , d ]枚举,然后利用容斥.详见代码: /****************************

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范围的. 所以显然不用去枚举每一个数了,更不用说推断每一个数是不是某个数的某次方.那么这个题怎么下手呢.首先我们能够非常直观的想到,被删去的数显然是非常分散的.由于能表示成某个数的幂这种形式的数非

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 ascen

[多校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 1695 GCD 欧拉函数+容斥定理

输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和1到d/k 2个区间 如果第一个区间小于第二个区间 讲第二个区间分成2部分来做1-b/k 和 b/k+1-d/k 第一部分对于每一个数i 和他互质的数就是这个数的欧拉函数值 全部数的欧拉函数的和就是答案 第二部分能够用全部数减去不互质的数 对于一个数i 分解因子和他不互质的数包括他的若干个因子 这个

HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法

题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd(a_1,a_2,a_3…a_n)=d$,显然每个$a_i$的倍数都满足,有$\frac{a_i}{d}$种方案 那么一个d对答案的贡献为\[\prod_{i=1}^{min(a)}{\lfloor\frac{a_i}{d}\rfloor}    \] 但是所有的d计入会有重复情况,考虑容斥,对d进行素数分