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


/* ***********************************************
Author        :CKboss
Created Time  :2015年08月09日 星期日 15时03分31秒
File Name     :HDOJ5297.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

LL ss[19]={0, -2, -3, -5, -7, -11, -13, -17, -19, -23, -29, -31, -37, -41, -43, -47, -53, -59, -61};

vector<LL> a;
LL n,r;

LL gao(LL x)
{
	LL ret=x;
	for(int i=0,sz=a.size();i<sz;i++)
	{
		LL cnt=(LL)pow(x+0.5,1.0/abs(a[i]))-1;
		if(a[i]<0) ret-=cnt;
		else ret+=cnt;
	}
	return ret-1;
}

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

	int T_T;
	cin>>T_T;
	while(T_T--)
	{
		cin>>n>>r;
		a.clear();
		for(int i=1;i<=18&&abs(ss[i])<=r;i++)
		{
			int k=a.size();
			for(int j=0;j<k;j++)
			{
				if(abs(a[j]*ss[i])>63) continue;
				LL t=a[j]*ss[i];
				a.push_back(t);
			}
			a.push_back(ss[i]);
		}

		LL ans=n;
		while(true)
		{
			LL left=gao(ans);
			if(left==n) break;
			ans=ans+n-left;
		}

		cout<<ans<<endl;
	}
    
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值