题解 P1829 【[国家集训队]Crash的数字表格 / JZPTAB】

2019-09-21 11:06:13


可能更好的阅读体验

这里写两种方法,一种是套路,一种是 $\mathsf{\color{black}{x}\color{red}{gzc}}$ 的神仙方法。

下文中,设 $n\leq m$ , $n/m=\left\lfloor\frac{n}{m}\right\rfloor$ 。

为了方便,设 $S(n)=\sum_{i=1}^ni=\frac{n(n+1)}{2}$ 。

方法一

$$\begin{aligned}&\sum_{i=1}^n\sum_{j=1}^m\operatorname{lcm}(i,j)\\=&\sum_{i=1}^n\sum_{j=1}^m\frac{ij}{\gcd(i,j)}\\=&\sum_{d=1}^n\frac{1}{d}\sum_{i=1}^{n}\sum_{j=1}^{m}ij[\gcd(i,j)=d]\\=&\sum_{d=1}^n\frac{1}{d}\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}idjd[\gcd(i,j)=1]\\=&\sum_{d=1}^nd\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}ij[\gcd(i,j)=1]\end{aligned}$$

$$\begin{aligned}\mathbf{f}(k)&=\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}ij[\gcd(i,j)=k]\\\mathbf{g}(k)&=\sum_{k|x}\mathbf{f}(x)\end{aligned}$$

莫比乌斯反演得

$$\mathbf{f}(1)=\sum_{1|k}\mu(k)\mathbf{g}(k)=\sum_{k=1}^{n/d}\mu(k)\mathbf{g}(k)$$

考虑求出 $\mathbf{g}(k)$ 。

$$\begin{aligned}\mathbf{g}(k)&=\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}ij[k|\gcd(i,j)]\\&=\sum_{i=1}^{n/dk}\sum_{j=1}^{m/dk}ikjk[1|\gcd(i,j)]\\&=k^2\sum_{i=1}^{n/dk}i\sum_{j=1}^{m/dk}j\\&=k^2S\left(\left\lfloor\frac{n}{dk}\right\rfloor\right)S\left(\left\lfloor\frac{m}{dk}\right\rfloor\right)\end{aligned}$$

代回去得到

$$\mathbf{f}(1)=\sum_{k=1}^{n/d}\mu(k)k^2S\left(\left\lfloor\frac{n}{dk}\right\rfloor\right)S\left(\left\lfloor\frac{m}{dk}\right\rfloor\right)$$

再代回去得到

$$\begin{aligned}\mathbf{Orig\ Equ}&=\sum_{d=1}^nd\sum_{k=1}^{n/d}\mu(k)k^2S\left(\left\lfloor\frac{n}{dk}\right\rfloor\right)S\left(\left\lfloor\frac{m}{dk}\right\rfloor\right)\\&=\sum_{T=1}^n\left(T\sum_{d|T}d\mu(d)\right)S\left(\left\lfloor\frac{n}{T}\right\rfloor\right)S\left(\left\lfloor\frac{m}{T}\right\rfloor\right)\end{aligned}$$

括号内预处理,然后数论分块计算即可。

方法二

令 $\mathbf{f}(i)=\frac{1}{i}$ , $\mathbf{g}(i)=\mathbf{f}*\mu$ 。

$$\begin{aligned}&\sum_{i=1}^n\sum_{j=1}^m\operatorname{lcm}(i,j)\\=&\sum_{i=1}^n\sum_{j=1}^m\frac{ij}{\gcd(i,j)}\\=&\sum_{i=1}^ni\sum_{j=1}^mj\,\mathbf{f}(\gcd(i,j))\\=&\sum_{i=1}^ni\sum_{j=1}^mj\sum_{d|i,d|j}\mathbf{g}(d)\\=&\sum_{d=1}^n\left(\sum_{k|d}\frac{\mu(k)}{\frac{d}{k}}\right)\sum_{i=1}^{n/d}id\sum_{j=1}^{m/d}jd\\=&\sum_{d=1}^n\left(d\sum_{k|d}k\mu(k)\right)\sum_{i=1}^{n/d}i\sum_{j=1}^{m/d}j\\=&\sum_{d=1}^n\left(d\sum_{k|d}k\mu(k)\right)S\left(\left\lfloor\frac{n}{d}\right\rfloor\right)S\left(\left\lfloor\frac{m}{d}\right\rfloor\right)\end{aligned}$$

括号内预处理,然后数论分块计算即可。

这么良心的题解真的不给个赞吗(

// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define re register
using namespace std;

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int N=10000000+10;
const int mod=20101009;

int p[N],v[N],cnt=0;
int f[N];

inline void sieve(int n) {
    f[1]=1;
    for (re int i=2;i<=n;++i) {
        if (!v[i]) p[++cnt]=i,f[i]=mod-i+1;
        for (re int j=1;j<=cnt&&i*p[j]<=n;++j) {
            v[i*p[j]]=1;
            if (i%p[j]) f[i*p[j]]=1ll*f[i]*f[p[j]]%mod;
            else { f[i*p[j]]=f[i]; break; }
        }
    }
    for (re int i=1;i<=n;++i) f[i]=1ll*f[i]*i%mod;
    for (re int i=1;i<=n;++i) f[i]=(f[i]+f[i-1])%mod;
}

inline int S(int n) { return 1ll*n*(n+1)/2%mod; }

int main() {
    int n=read(),m=read(); if (n>m) swap(n,m);
    sieve(n); int ans=0;
    for (re int l=1,r;l<=n;l=r+1) {
        r=min(n/(n/l),m/(m/l));
        ans=(ans+1ll*(f[r]-f[l-1]+mod)%mod*S(n/l)%mod*S(m/l))%mod;
    }
    printf("%d\n",ans);
    return 0;
}