Codeforces - 920G - List Of Integers

Codeforces - 920G - List Of Integers

题意是求大于x且与p互质的第k个数。
不大于num且与p互质的数的个数=num-不大于num且与p有公因子的数的个数,这样就可以用容斥求了:如p有素因子ABC
有公因子A的个数+有公因子B的个数+有公因子C的个数-有公因子AB-有公因子BC-有公因子AC+有公因子ABC

由于p最多有7个公因子,容斥的复杂度是 O(727) O ( 7 ∗ 2 7 ) 。分解素因子的复杂度是 1000 1000 ,因此就可以高了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int x,k,p;
vector<ll> fac;
void getfac()
{
    fac.clear();
    int tmp = p;
    for(int i=2;i*i<=tmp;++i)
    {
        if(tmp%i==0)
        {
            fac.push_back(i);
            while(tmp%i==0) tmp/=i;
        }
    }
    if(tmp!=1) fac.push_back(tmp);
}
int check(ll num)
{
    //不大于num且与p互质的数=num-与p有相同因子的数
    //与p有相同因子(ABC)的数=A+B+C-AB-AC-BC+ABC ...
    ll res=0;
    int c=1;
    int sz=fac.size();
    for(int i=1;i<(1<<sz);++i)
    {
        int v = __builtin_popcount(i);
        ll mul = 1;
        for(int j=0;j<sz;++j)
            if((i>>j)&1) mul*=fac[j];
        res += ((v&1)?1:-1)*(num/mul-x/mul);
    }
    res = num-x-res;
    return res;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&x,&p,&k);
        int l=x+1,r=1e9;
        getfac();
        while(l<r)
        {
            int m=(l+r)>>1;
            if(check(m)<k) l=m+1;
            else r=m;
        }
        printf("%d\n",r);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值