P1829 [国家集训队]Crash的数字表格 / JZPTAB
Desciption
题目链接:P1829
给定 $n,m$,求
$$(\sum\limits_{i=1}^n\sum\limits_{j=1}^mlcm(i,j)) \bmod 20101009$$
$1\leq n,m\leq 10^7$
Solution
首先抛开模数,
$$\sum\limits_{i=1}^n\sum\limits_{j=1}^mlcm(i,j)$$
根据最小公倍数定义,
$$=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\frac{ij}{gcd(i,j)}$$
然后常见套路,枚举 $gcd$,
$$=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\sum\limits_{d=1}^n\frac{ij}{d}[gcd(i,j)=d]$$
改变枚举顺序,把 $gcd(i,j)=d$ 消成 $gcd(i,j)=1$,
$$=\sum\limits_{d=1}^n\sum\limits_{i=1}^{\lfloor\frac{n}{d} \rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}dij[gcd(i,j)=1]$$
然后根据莫比乌斯函数性质,
$$=\sum\limits_{d=1}^n\sum\limits_{i=1}^{\lfloor\frac{n}{d} \rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}\sum\limits_{kgcd(i,j)}\mu(k)dij$$
改变枚举顺序,
$$=\sum\limits_{d=1}^nd\sum\limits_{k=1}^{\lfloor{\frac{n}{d}}\rfloor}\mu(k)k^2\sum\limits_{i=1}^{\lfloor \frac{n}{dk}\rfloor}\sum\limits_{j=1}^{\lfloor \frac{m}{dk}\rfloor}ij$$
然后根据结合律,
$$=\sum\limits_{d=1}^nd\sum\limits_{k=1}^{\lfloor{\frac{n}{d}}\rfloor}\mu(k)k^2(\sum\limits_{i=1}^{\lfloor \frac{n}{dk}\rfloor}i)(\sum\limits_{j=1}^{\lfloor \frac{m}{dk}\rfloor}j)$$
那么令 $F(x)=\mu(x)x^2$,$G(x)=\sum\limits_{i=1}^xi$,由于其均是积性函数,故
$$=\sum\limits_{d=1}^nd\sum\limits_{k=1}^{\lfloor{\frac{n}{d}}\rfloor}F(k)G(\lfloor \frac{n}{dk}\rfloor)G(\lfloor \frac{m}{dk}\rfloor)$$
然后 $F(x)$ 直接筛的时候处理即可,$G(x)$ 为等差数列,直接 $\mathcal O(1)$ 求出,最后用整除分块优化即可。
时间复杂度:$\mathcal O(N)$($\sum\limits_{i=1}^N \sqrt\frac{N}{i}\approx N$)
Code
1 |
|