Simple math - Mobius inversion, Du teach sieve, divisible block

\[\begin{split} A&=\sum_{i=1}^{n}\sum_{j=1}^n{ijgcd(i,j)} \\ &=\sum_{d=1}^n\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}ijd^3[gcd(i,j)=1]\\ &=\sum_{d=1}^nd^3\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}ij[gcd(i,j)=1] \end{split} \]
\(f(n)=\sum_{i=1}^{n}\sum_{j=1}^nij[gcd(i,j)=1]\)\(s(n)=\sum_{i=1}^{n}\sum_{j=1}^nij\)


\[\begin{split}A&=\sum_{d=1}^nd^3f(\lfloor\frac{n}{d}\rfloor)\end{split}\]

and

\[\sum_{i=1}^{n}i^3=(\sum_{i=1}^ni)^2=[\frac{n(n+1)}{2}]^2\]

Have

\[\begin{split}s(n)&=\sum_{i=1}^{n}\sum_{j=1}^nij=[\frac{n(n+1)}{2}]^2\\ &=\sum_{i=1}^{n}\sum_{j=1}^nij \\ &=\sum_{t=1}^{n}\sum_{i=1}^{\lfloor\frac{n}{t}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{t}\rfloor}t^2ij[gcd(i,j)=1] \\ &=\sum_{t=1}^{n}t^2f(\lfloor\frac{n}{t}\rfloor)=f(n)+\sum_{t>1}t^2f(\lfloor\frac{n}{t}\rfloor) \end{split}\]

\[f(n)=s(n)-\sum_{t>1}t^2f(\lfloor\frac{n}{t}\rfloor)=[\frac{n(n+1)}{2}]^2-\sum_{t>1}t^2f(\lfloor\frac{n}{t}\rfloor)\]

On the block number to
\ [\ sum_ {i = 1 } ^ {n} i ^ 2 = \ frac {n (n + 1) (2n + 1)} {6} \]

预处理 \(f(n)\)
\[f(n)=\sum_{i=1}^{n}\sum_{j=1}^nij[gcd(i,j)=1]=f(n-1)+2\sum_{i=1}^{n}in[gcd(i,n)=1]\]


\ [\ sum_ {i = 1} ^ or [GCD (i, n) = 1] n = \ frac {\ varphi (n)} {2} \]


\, [f, (n), = f, (n-the 1), the 2 +, n ^ \ varphi, (n), \]

Mentally disabled athletes accidentally n also die out so tune more than half an hour

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 5e6+5;

int i2,i4,i6,mod,n;

bool isNotPrime[N + 5];
int mu[N + 5], phi[N + 5], primes[N + 5], cnt;
inline void euler() {
    isNotPrime[0] = isNotPrime[1] = true;
    mu[1] = 1;
    phi[1] = 1;
    for (int i = 2; i <= N; i++) {
        if (!isNotPrime[i]) {
            primes[++cnt] = i;
            mu[i] = -1;
            phi[i] = i - 1;
        }
        for (int j = 1; j <= cnt; j++) {
            int t = i * primes[j];
            if (t > N) break;
            isNotPrime[t] = true;
            if (i % primes[j] == 0) {
                mu[t] = 0;
                phi[t] = phi[i] * primes[j];
                break;
            } else {
                mu[t] = -mu[i];
                phi[t] = phi[i] * (primes[j] - 1);
            }
        }
    }
}

inline void exgcd(int a,int b,int &x,int &y) {
    if(!b) {
        x=1,y=0;
        return;
    }
    exgcd(b,a%b,x,y);
    int t=x;
    x=y,y=t-(a/b)*y;
}

inline int inv(int a,int b) {
    int x,y;
    return exgcd(a,b,x,y),(x%b+b)%b;
}

int sum[N+1];

int h(int n) { n%=mod;
    return n*n%mod*(n+1)%mod*(n+1)%mod*i4%mod;
}

int u(int n) { n%=mod;
    return n*(n+1)%mod*(2ll*n+1)%mod*i6%mod;
}

map<int,int> mp;

int S(int n) {
    if(n<N) return sum[n];
    if(mp[n]) return mp[n];
    int l=2, r, ans=h(n);
    while(l<=n) {
        r=n/(n/l);
        ans -= (((u(r)-u(l-1))%mod+mod)%mod*S(n/l))%mod;
        ans %= mod;
        ans += mod;
        ans %= mod;
        l=r+1;
    }
    mp[n]=ans;
    return ans;
}

int read() {
    long long t;
    cin>>t;
    return t;
}

signed main() {
    ios::sync_with_stdio(false);
    mod=read();
    n=read();
    i2=inv(2,mod);
    i4=inv(4,mod);
    i6=inv(6,mod);
    euler();
    sum[0]=phi[0];
    for(int i=1;i<=N;i++) sum[i]=(sum[i-1]+phi[i]%mod*i%mod*i%mod)%mod;
    int ans = 0, l=1, r;
    while(l<=n) {
        //cout<<l<<endl;
        r=(n/(n/l));
        ans += h(n/l) * ((S(r)-S(l-1)+mod)%mod) % mod;
        ans %= mod;
        l=r+1;
    }
    cout<<(long long)ans;
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12326505.html