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
 

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值