HDU 5297 Y sequence (容斥原理 +迭代)

题意:自然数集去除能开b (2<b<63) 次方的数。

如果能知道 1~ x 之间有多少个符合要求的数,问题就有解法的办法了,即迭代扩大x的范围,如果里面的数的个数正好等于n,x即是答案。

所以想办法求出1 ~ x 之间有多少符合要求的数。

自然数集 去掉 能开1,2,3,4,5……次方的数,可以依次先去掉能开1次的,2次的,3次,4次……在2e18范围内只要的数并不多。

但是显然,这样有些数被重复减掉了多次,比如去掉能开2次方的时候,4次、8次的肯定也被去掉了,分析之后可以发现: b的质因子个数为奇数减去,质因子个数为偶数的要加回来,质因子个数大于1且都是同的不用管。即容斥原理。

所以先预处理一下可能出现的幂,然后对于一个x,在[1,x]之间能开b次方的数的个数为:pow( x , 1/b),

【代码】

/* ***********************************************
Author        :angon

************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define lld %I64d
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scanl(d) scanf("%I64d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define scannl(n,m) scanf("%I64d%I64d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a))
#define LL long long
#define N 1005
#define mod 1000000007
inline int read(){int s=0;char ch=getchar();for(; ch<'0'||ch>'9'; ch=getchar());for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';return s;}

int prime[20] = {-2,-3,-5,-7,-11,-13,-17,-19,-23,-29,-31,-37,-41,-43,-47,-53,-59,-61,-67};
vector<int>b;

void get_b(int r)
{
    b.clear();
    for(int i=0; abs(prime[i]) <= r;i++)
    {
        int sz = b.size();
        for(int j=0; j<sz;j++)
        {
            if(abs(b[j]*prime[i]) < 63)
            {
                //printf("--%d\n",b[j]*prime[i]);
                b.push_back(b[j]*prime[i]);
            }
        }
        b.push_back(prime[i]);
        //printf("``%d\n",prime[i]);
    }
}

LL count_Y(LL x)
{
    LL res = x;
    for(int i=0; i<b.size();i++)
    {
        if(b[i]<0) res -= (LL)pow(x + 0.5,1.0/abs(b[i])) - 1;
        else       res += (LL)pow(x + 0.5,1.0/abs(b[i])) - 1;
    }
    return res - 1;
}

LL slove(LL n,int r)
{
    get_b(r);
    LL ans = n;
    while(1)
    {
        LL temp = count_Y(ans);
        if(temp == n) break;
        ans += n - temp;
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    printf("%lf\n",sqrt(4096));
    int t; scan(t);
    while(t--)
    {
        LL n;int r;
        scanl(n); scan(r);
        printf("%lld\n",slove(n,r));
    }


    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值