Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

容斥+二分 0ms

Posted by ALLACS at 2019-06-03 17:12:00 on Problem 2773
//容斥+二分
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100+5;
const int INF=0x3f3f3f3f;
typedef long long LL;
LL k;
LL fac[maxn],cnt;
//利用容斥求指定区间[1,n]相对素数数量
LL sum,solve;
void dfs(int pos,int num,LL n,LL val,int now)
{
    if(now==num) {
        sum+=n/val;
        return;
    }
    for(int i=pos;i<cnt;i++)
    {
        dfs(i+1,num,n,val*1l*fac[i],now+1);
    }
}
LL realiveprime(LL n)
{
    LL ans=0;
    for(int i=1;i<=cnt;i++)
    {
        sum=0;
        dfs(0,i,n,1,0);
        if(i&1) ans+=1l*sum;
        else ans-=1l*sum;
   }
   return n-ans;
}
void binsearch(LL l,LL r)
{
    while(l<r) {
        LL mid1 = (l+r)>>1;
        LL mid = realiveprime(mid1);
        if(k>mid) {
            l = mid1+1;
        }
        else if(k==mid) {
            solve=min(solve,mid1);
            r=mid1;
        }
        else if(k<mid) {
            r = mid1;
        }
    }
}
int main()
{
    LL m;
    while(scanf("%lld%lld",&m,&k)==2)
    {
        solve=INF;
        //对m进行唯一分解
        cnt=0;
        for(LL i=2;1l*i*i<=m;i++)
        {
            if(m%i==0) {
                fac[cnt]=i;
                while(m%i==0) {
                    m/=i;
                }
                cnt++;
            }
            if(m==1) break;
        }
        if(m!=1) {
            fac[cnt]=m;
            cnt++;
        }
//        for(int i=0;i<cnt;i++)
//            printf("%d\n",fac[i]);
        //寻找m的第k个相对素数
        binsearch(1,10000000000);
        printf("%lld\n",solve);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator