POJ 2773 Happy 2006 二分+容斥

占坑ing

#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 mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&(-x))
typedef long long LL;
#define maxn 10005
const int inf=(1<<28)-1;
int fac[maxn],len;
void get_fac(LL n)
{
    len=0;
    LL t=n;
    for(int i=2;i*i<=t;++i)
    if(t%i==0)
    {
        fac[++len]=i;
        while(t%i==0) t/=i;
    }
    if(t>1) fac[++len]=t;
}
LL m,k,ans;
void dfs(int pos,int tot,LL val,LL x)
{
    if(pos==len+1)
    {
        if(!tot) return ;
        if(tot&1) ans+=x/val;
        else ans-=x/val;
        return ;
    }
    dfs(pos+1,tot+1,val*fac[pos],x);
    dfs(pos+1,tot,val,x);
}
bool cheak(LL x)
{
    ans=0;
    dfs(1,0,1,x);
    return (x-ans)>=k;
}
int main()
{
    while(~scanf("%lld%lld",&m,&k))
    {
        get_fac(m);
        LL ans=-1,l=0,r=inf;
        r*=inf;
        while(l<=r)
        {
            LL mid=(l+r)/2;
            if(cheak(mid))
            {
                r=mid-1;
                ans=mid;
            }
            else l=mid+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(POJ 2773 Happy 2006 二分+容斥)