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

32 篇文章 0 订阅

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5297


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




题意:给出序列1,2,3...MAX; 去掉其中可以表示为a^b(2<=b<=r)的数,求出新序列中第n项是什么。

题解:首先,知道x^(1/b)表示x范围内有多少个a^b次方的数,容斥幂就可以求出r内的解,

      由于MAX很大,二分会超时,用迭代来做,令ans=n,求出上限为ans的序列长度len,显然,len<=n

      上限为ans的序列中有len个数,然后将上限加n-len,逐渐逼近于答案。




//#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define bug cout<<"bug"<<endl
int r;
int prime[107],np;
vector<int> mi;
void get_prime()
{
    np=0;
    for(int i=2; i<=100; ++i)
    {
        int f=1;
        for(int j=0; j<np; ++j)
            if(i%prime[j]==0)f=0;
        if(f)prime[np++]=-i;
    }
}
void getr()
{
    mi.clear();
    for(int i=0; -prime[i]<=r; ++i)
    {
        int all=mi.size();
        for(int j=0; j<all; ++j)
            if(abs(mi[j]*prime[i])<=62)
            mi.push_back(mi[j]*prime[i]);
        mi.push_back(prime[i]);
    }
}
long long cal(long long x)
{
    long long ans=0;
    int all=mi.size();
    for(int i=0; i<all; ++i)
    {
        int b=mi[i];
        long long temp=pow(double(x+0.5),1.0/abs(b))-1;
        if(b<0)ans+=temp;
        else ans-=temp;
    }
    return x-ans-1;
}
int main()
{
    int T;
    long long n;
    get_prime();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d%d",&n,&r);
        getr();
        long long temp=cal(n),ans=n;
        while(temp<n)
        {
            ans+=n-temp;
            temp=cal(ans);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
/*
2
10 2
10 3
*/






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值