#莫比乌斯反演#BZOJ 2671 洛谷 4466 和与积 Calc

题目

求有多少对 ( a , b ) (a,b) (a,b)满足 1 ≤ a < b ≤ n 1\leq a<b\leq n 1a<bn a + b ∣ a b a+b|ab a+bab


分析

g c d ( a , b ) = 1 gcd(a,b)=1 gcd(a,b)=1,那么 a + b ∤ a b a+b∤ab a+bab
g c d ( a , b ) ≠ 1 gcd(a,b)\neq 1 gcd(a,b)̸=1,设 d = g c d ( a , b ) , i = d a , j = d b d=gcd(a,b),i=da,j=db d=gcd(a,b),i=da,j=db
∴ ( i + j ) ∣ i j d \therefore (i+j)|ijd (i+j)ijd
∵ \because g c d ( a , b ) = 1 gcd(a,b)=1 gcd(a,b)=1,那么 a + b ∤ a b a+b∤ab a+bab
∴ ( i + j ) ∣ d \therefore (i+j)|d (i+j)d
也就是求有多少个三元组 ( i , j , d ) (i,j,d) (i,j,d),满足 i d , j d ≤ n , g c d ( i , j ) = 1 , ( i + j ) ∣ d id,jd\leq n,gcd(i,j)=1,(i+j)|d id,jdn,gcd(i,j)=1,(i+j)d
那么 = ∑ i = 1 n ∑ j = 1 i − 1 ⌊ ⌊ n i ⌋ i + j ⌋ [ g c d ( i , j ) = 1 ] =\sum_{i=1}^{n}\sum_{j=1}^{i-1}\lfloor\frac{\lfloor\frac{n}{i}\rfloor}{i+j}\rfloor[gcd(i,j)=1] =i=1nj=1i1i+jin[gcd(i,j)=1]
= ∑ i = 1 n ∑ j = 1 i − 1 ⌊ n i ( i + j ) ⌋ [ g c d ( i , j ) = 1 ] =\sum_{i=1}^{n}\sum_{j=1}^{i-1}\lfloor\frac{n}{i(i+j)}\rfloor[gcd(i,j)=1] =i=1nj=1i1i(i+j)n[gcd(i,j)=1]
∑ k ∣ n μ ( k ) = [ n = 1 ] \sum_{k|n}\mu(k)=[n=1] knμ(k)=[n=1]
也就是 = ∑ i = 1 n ∑ j = 1 i − 1 ⌊ n i ( i + j ) ⌋ ∑ k ∣ i , k ∣ j μ ( k ) =\sum_{i=1}^{n}\sum_{j=1}^{i-1}\lfloor\frac{n}{i(i+j)}\rfloor\sum_{k|i,k|j}\mu(k) =i=1nj=1i1i(i+j)nki,kjμ(k)
枚举 k k k可得
= ∑ k = 1 n μ ( k ) ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 i − 1 ⌊ n i k ( i k + j k ) ⌋ =\sum_{k=1}^{n}\mu(k)\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{i-1}\lfloor\frac{n}{ik(ik+jk)}\rfloor =k=1nμ(k)i=1knj=1i1ik(ik+jk)n
= ∑ k = 1 n μ ( k ) ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 i − 1 ⌊ ⌊ n i k 2 ⌋ i + j ⌋ =\sum_{k=1}^{n}\mu(k)\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{i-1}\lfloor\frac{\lfloor\frac{n}{ik^2}\rfloor}{i+j}\rfloor =k=1nμ(k)i=1knj=1i1i+jik2n
Wait,这样不会更慢吗?可以发现 i , j , k i,j,k i,j,k的取值只能在 n \sqrt n n 范围内,所以
= ∑ k = 1 n μ ( k ) ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 i − 1 ⌊ ⌊ n i k 2 ⌋ i + j ⌋ =\sum_{k=1}^{\sqrt n}\mu(k)\sum_{i=1}^{\lfloor\frac{\sqrt n}{k}\rfloor}\sum_{j=1}^{i-1}\lfloor\frac{\lfloor\frac{n}{ik^2}\rfloor}{i+j}\rfloor =k=1n μ(k)i=1kn j=1i1i+jik2n
洛谷dalao证明时间复杂度是 O ( n log ⁡ n ) O(\sqrt n \log n) O(n logn),可谓是非常高级了


代码

#include 
#include 
#define rr register
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef unsigned uit;
const uit N=47001;
uit prime[N>>3],cnt,n;
int mu[N]; bool v[N];
inline long long answ(uit x,uit y){
    rr long long ans=0; rr uit t=x<<1;
    for(rr uit l=x+1,r;l<t;l=r+1){
        if(!(y/l)) return ans;
        r=min(y/(y/l),t-1);
        ans+=(r-l+1)*(y/l);
    }
    return ans;
}
signed main(){
    scanf("%u",&n);
    rr uit m=sqrt(n); rr long long ans=0;
    for (rr uit i=1;i<=m;++i){
        if (i>1){
            if (!v[i]) prime[++cnt]=i,mu[i]=-1;
            for (rr uit j=1;j<=cnt&&prime[j]*i<=m;++j){
                v[i*prime[j]]=1;
                if (i%prime[j]) mu[i*prime[j]]=-mu[i];
                    else break;
            }
        }else mu[i]=1;
        if (!mu[i]) continue;
        for (rr uit j=1;j*i<=m;++j)
            ans+=answ(j,n/(i*i*j))*mu[i];
    }
    printf("%lld",ans);
    return 0;
}

你可能感兴趣的:(反演)