题意:
求第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;
}