luogu P2398 [GCD SUM]

Front cheese

  • Number theory block, a little inversion Mobius

text

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

Then go heavy.

It is contemplated that the enumeration \ (\ n) within \ (the p-\) , the contribution of the answers into \ (p \ times gcd (i , j) = p \) the number of the number of

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

Simplification formula

\[\sum_{p=1}^{n}p\sum_{i=1}^{\left \lfloor \frac{n}{p} \right \rfloor }\sum_{j=1}^{\left \lfloor \frac{n}{p} \right \rfloor }[gcd(i,j)=1]\]

Since \ ([gcd (i, j ) = 1] \) only \ (gcd (i, j) = 1 \) have \ (1 \) contributions or not, it can be replaced with \ (\ varepsilon (gcd (i, j)) \)

\ (\ varepsilon (x) \ ) is, when \ (x = 1 \) when answers contributions \ (1 \) , otherwise \ (0 \)

So now converted to

\[\sum_{p=1}^{n}p\sum_{i=1}^{\left \lfloor \frac{n}{p} \right \rfloor }\sum_{j=1}^{\left \lfloor \frac{n}{p} \right \rfloor }\varepsilon(gcd(i,j))\]

By the \ (Dirichlet \) was convolution

\(\varepsilon =\mu*1 \Leftrightarrow \varepsilon(n)=\sum_{d|n}\mu(d)\)

So the original formula is converted to
\ [\ sum_ {p = 1 } ^ {n} p \ sum_ {i = 1} ^ {\ left \ lfloor \ frac {n} {p} \ right \ rfloor} \ sum_ {j = 1} ^ {\ left \ lfloor \ frac {n} {p} \ right \ rfloor} \ sum_ {d | gcd (i, j)} \ mu (d) \]

Transforming sums order

\[\sum_{p=1}^{n}p\sum_{d=1}{\mu(d)}\sum_{i=1}^{\left \lfloor \frac{n}{p} \right \rfloor }[d\ |\ i]\sum_{j=1}^{\left \lfloor \frac{n}{p} \right \rfloor}[d\ | \ j]\]

The latter two requirements is the \ (\ left \ lfloor \ frac {n} {p} \ right \ rfloor \) in \ (D \) multiple, easily know the answer is \ (\ left \ lfloor \ frac {n } {pd} \ right \ rfloor \)

So the answer into

\[ans=\sum_{p=1}^{n}p\sum_{d=1}\mu(d)\left \lfloor \frac{n}{pd} \right \rfloor\left \lfloor \frac{n}{pd} \right \rfloor\]

And that thing back block and can use the prefix number theory.

\(Code:\)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define int long long 
inline int read(){
    register int x=0,f=0,ch=getchar();
    while('0'>ch||ch>'9')f^=ch=='-',ch=getchar();
    while('0'<=ch&&ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=getchar();
    return f?-x:x;
}
const int MAX=100005;
int n,tot,p[MAX],f[MAX],mu[MAX];
inline void Gmu(){
    mu[1]=1;tot=0;
    for(register int i=2;i<=n;++i){
        if(!f[i]){
            p[++tot]=i;
            mu[i]=-1;
        }
        for(register int j=1;j<=tot&&i*p[j]<=n;++j){
            f[i*p[j]]=1;
            if(i%p[j]==0){
                mu[i*p[j]]=0;
                break;
            }
            mu[i*p[j]]=-mu[i];
        }
    }
    for(register int i=1;i<=n;++i)mu[i]+=mu[i-1]; 
} 
int res,ans;
inline void solve(int n,int p){
    res=0;
    for(register int l=1,r;l<=n;l=r+1){
        r=n/(n/l);
        res+=(mu[r]-mu[l-1])*(n/l)*(n/l);
    }
    ans+=res*p;
}
signed main(){
    n=read();
    Gmu();
    for(register int p=1;p<=n;++p)solve(n/p,p);
    printf("%lld\n",ans);
    return 0;
}

What's written questions trouble private letter, thank you

Guess you like

Origin www.cnblogs.com/Lates/p/12529883.html
gcd