容斥原理

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 7;
int a[N];
LL ans,all;

void dfs(int x, LL y){
    if(x>a[0]){ans+=all/y; return;}
    dfs(x+1,y); dfs(x+1,-y*a[x]);
}

LL count(LL x){
    ans=0,all=x;
    dfs(1,1);
    return ans;
}

int main(){
    int m,k;
    while(!cin.eof()){
        cin>>m>>k;
        a[0]=0;
        for(int i=2;i*i<=m;++i)
            if(m%i==0){
                a[++a[0]]=i;
                for(;m%i==0;m/=i);
            }
        if(m>1)a[++a[0]]=m;
        LL l=1,r=1e18,mid;
        for(;l<=r;){
            mid=(l+r)>>1;
            if(count(mid)>=k)r=mid-1;
            else l=mid+1;
        }
        cout<<l<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/codetogether/p/7213829.html