Mobius inversion simple application # 1

Mobius inversion formula we have derived before for:

\(f(n)=\sum_{d|n}g(d),g(n)=\sum_{d|n}f(d)\times\mu(\frac n d)\)

Specific derivation can refer to my previous one blog .

And before we derive the Mobius inversion of this formula, he had received another equally important equation:

\(\sum_{d|n}\mu(d)=[n=1]\)

That is, \ (\ * 1 MU = \ Epsilon \) .

This equation dim it?

We convert it to a little bit, so that \ (the n-= gcd (i, J) \) , this is the case, the equation becomes:

\(\sum_{d|gcd(i,j)}\mu(d)=[gcd(i,j)=1]\)

And we can exchange (\ Sigma \) \ to achieve some similar location \ (gcd (i, j) = 1 \) to solve the equation.

Example 1:

求:\(\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=1]\)

This is a specific application of the formulas I said above it, with the above formula, this problem is very simple matter.

\(\ \ \ \ \sum_{i=1}^n\sum_{j=1}^{m}[gcd(i,j)=1]\)

\(=\sum_{i=1}^n\sum_{i=1}^m\sum_{d|gcd(i,j)}\mu(d)\)

\(=\sum_{d=1}^n\mu(d)\sum_{i=1}^{\lfloor\frac n d\rfloor}\sum_{j=1}^{\lfloor\frac m d\rfloor}\)

\(=\sum_{d=1}^n\mu(d)\times\lfloor\frac n d\rfloor\times\lfloor\frac m d\rfloor\)

Then it may be (O (\ sqrt {n} ) \) \ solved in time.

Because Example 1, the code is posted, when the demonstration:

#include<bits/stdc++.h>
#define ll long long
const int N=10000000;
int mu[N+5],vis[N+5],sum[N+5],p[N+5];
ll ans;
int n,m,q;
using namespace std;
void sieve(){
    mu[1]=vis[1]=1;
    for(int i=2;i<=N;i++){
        if (!vis[i]) p[++p[0]]=i,mu[i]=-1;
        for (int j=1;j<=p[0]&&i*p[j]<=N;j++){
            vis[i*p[j]]=1;
            if (i%p[j]==0){
                mu[i*p[j]]=0;
                break;
            }
            mu[i*p[j]]=-mu[i];
        }
    }
    for (int i=1;i<=N;i++)
        mu[i]+=mu[i-1];
}
int main(){
    sieve();
    scanf("%d",&q);
    while (q--){
        scanf("%d%d",&n,&m);
        if (n>m) swap(n,m);
        ans=0;
        for (int l=1,r;l<=n;l=r+1){
            r=min(n/(n/l),m/(m/l));
            ans+=1ll*(mu[r]-mu[l-1])*(n/l)*(m/l);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Example 2:

求:\(\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=k]\)

In fact, here only need a simple transformation:

\(\ \ \ \ \sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=k]\)

\(=\sum_{i=1}^{\lfloor\frac n d\rfloor}\sum_{j=1}^{\lfloor\frac m d\rfloor}[gcd(i,j)=1]\)

Then the same as above it.

Example 3:

求:\(\sum_{i=1}^n\sum_{j=1}^mi\times j\times[gcd(i,j)=k]\)

This and the above, it is no big difference, here \ (i, j \) is only a by-product, as long as we shift \ (\ Sigma \) when not forget to \ (i, j \) impact of items generated that is can.

\(\ \ \ \ \sum_{i=1}^n\sum_{j=1}^mi\times j\times[gcd(i,j)=k]\)

\(=\sum_{i=1}^{\lfloor\frac n d\rfloor}\sum_{j=1}^{\lfloor\frac m d\rfloor}i\times j\times k^2\times[gcd(i,j)=1]\)

\(=k^2\times\sum_{d=1}^{\lfloor\frac n d\rfloor}\mu(d)\times\sum_{i=1}^{\lfloor\frac n d\rfloor}i\times\sum_{j=1}^{\lfloor\frac m d\rfloor}j\)

Decree \ (sum [n] = \ sum_ {i = 1} ^ ni \) .

\(=k^2\times\sum_{d=1}^{\lfloor\frac n d\rfloor}\mu(d)\times sum[\lfloor\frac n d\rfloor]\times sum[\lfloor\frac m d\rfloor]\)

\ (O (\ sqrt {n }) \) Solution to.

To so much of it, and the rest, there are many, after all, Mobius inversion profound, after the write.

Guess you like

Origin www.cnblogs.com/WR-Eternity/p/10990125.html