容斥原理1005 POJ 2773

题意:
求第k个与m互质的数
思路:
k可能很大,Ans可能>m
我们可以二分1~mid与m互质的数的个数>=k
然后就是比较简单的容斥了

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long LL;
const int maxn = 100005;
const int inf=(1<<28)-1;
int Fac[50];
void GetFac(int x)
{
    Fac[0]=0;
    for(int i=2;i*i<=x;++i)
    if(x%i==0)
    {
        Fac[++Fac[0]]=i;
        while(x%i==0) x/=i;
    }
    if(x>1) Fac[++Fac[0]]=x;
}

LL dfs(int pos,LL Now,int step,LL m)
{
    if(Now>m) return 0;
    LL Ans=0;
    if(pos==Fac[0]+1)
    {
        if(Now==1) return 0;
        Ans=m/Now;
        if(step&1)
            return Ans;
        else
            return -Ans;
    }
    Ans+=dfs(pos+1,Now,step,m);
    Ans+=dfs(pos+1,Now*Fac[pos],step+1,m);
    return Ans;
}
LL n,k;
bool cheak(LL x)
{
    LL Ans;
    Ans=dfs(1,1,0,x);
    return (x-Ans)>=k;
}
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        GetFac(n);
        LL Ans=-1,l=1,r=inf;
        r*=r;
        while(l<=r)
        {
            LL mid=(l+r)/2;
            if(cheak(mid))
            {
                Ans=mid;
                r=mid-1;
            }
            else
                l=mid+1;
        }
        printf("%lld\n",Ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值