[Mobius function] [Inclusion and Exclusion] BZOJ 2440 perfect square

https://www.luogu.org/problem/P4318

analysis

M can be divided into two requirements [1, m] of the square of the number of non-factor

Obviously repellent may be accommodated: the square of the number of -1 the number of the square of a prime number of prime numbers 0 to +2 ......

Capacity factor is easy to find repellent function Mobius

Linear half screen to preconditioning

Note that two points l, r, mid range may exceed longlong

The final formula:

$ans=\sum _{i=1}^{\left \lfloor \sqrt{n} \right \rfloor} \mu (i)\left \lfloor \frac{n}{i^2} \right \rfloor$

 

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int Inf=2147483647;
const int N=46341;
int T,k;
int prime[N],miu[N],cnt;
bool nonprime[N];

void Prime() {
    miu[1]=1;
    for (int i=2;i<N;i++) {
        if (!nonprime[i]) prime[++cnt]=i,miu[i]=-1;
        for (int j=1;j<=cnt;j++) {
            if (i*prime[j]>=N) break;
            nonprime[i*prime[j]]=1;
            if (i%prime[j]==0) {
                miu[i*prime[j]]=0;
                break;
            }
            miu[i*prime[j]]=-1*miu[i];
        }
    }
}

bool Check(ll mid) {
    ll n=sqrt(mid),ans=0;
    for (int i=1;i<=n;i++) ans+=1ll*miu[i]*(mid/(i*i));
    return ans>=k;
}

int main() {
    Prime();
    for (scanf("%d",&T);T;T--) {
        scanf("%d",&k);
        ll l=1,r=Inf,mid,ans;
        while (l<=r) {
            mid=1ll*(l+r)>>1;
            if (Check(mid)) ans=mid,r=mid-1;
            else l=mid+1;
        }
        printf("%lld\n",ans);
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/mastervan/p/11367343.html