对于部分莫比乌斯反演的套路优化
引入
考虑题目,给定 \(k,n,m\leq 10^5\) 求解 \(\displaystyle \sum_{i=1}^n\sum_{j=1}^m \gcd(i,j)^k\) (方便起见,设 \(n\leq m\) )。
对于常规反演方法:优先考虑枚举最大质因数 \(g\) ,再利用 \([gcd(i,j)=g]\) 变换出反演式。具体步骤如下:
\(\displaystyle =\sum_{g=1}^ng^k\sum_{i=1}^n\sum_{j=1}^m [\gcd(i,j)=g]\)
\(\displaystyle =\sum_{g=1}^ng^k\sum_{i=1}^{n/g}\sum_{j=1}^{m/g}[\gcd(i,j)=1]\)
\(\displaystyle =\sum_{g=1}^ng^k\sum_{i=1}^{n/g}\sum_{j=1}^{m/g}\sum_{d\mid i\wedge d\mid j}\boldsymbol \mu(d)\)
\(\displaystyle =\sum_{g=1}^ng^k\sum_{d=1}^{n/g}\boldsymbol \mu(d)\sum_{i=1}^{n/g}[d\mid i]\sum_{j=1}^{m/g}[d\mid j]\)
\(\displaystyle =\sum_{g=1}^ng^k\sum_{d=1}^{n/g}\boldsymbol \mu(d)(n/gd)(m/gd)\)
令 \(T=gd\) ,优先枚举 \(T\) 则
\(\displaystyle =\sum_{T=1}^n\sum_{g\mid T}g^k\boldsymbol \mu({T\over g})(n/T)(m/T)\)
由于 \(\displaystyle \sum_{g\mid T}g^k\boldsymbol \mu({T\over g})\) 为两积性函数的狄利克雷卷积,故也为积性函数,记为 \(\boldsymbol f(T)\)
\(\displaystyle =\sum_{T=1}^n\boldsymbol f(T)(n/T)(m/T)\)
之后线性筛+整除分块即可
但该套路的推演方案实则有大量笔墨可以优化。
优化
优先考虑一个积性函数 \(\boldsymbol f\) ,考虑其狄利克雷卷积
\(\boldsymbol f=\boldsymbol f*\boldsymbol \varepsilon=\boldsymbol f*(\boldsymbol \mu*\boldsymbol I)=(\boldsymbol f*\boldsymbol \mu)*\boldsymbol I\)
故 \(\displaystyle \boldsymbol f(n)=[(\boldsymbol f*\boldsymbol \mu)*\boldsymbol I](n)=\sum_{d\mid n}(\boldsymbol f*\boldsymbol \mu)(d)\cdot \boldsymbol I({n\over d})\)
由于 \(\boldsymbol I(n)=1\) ,故 \(\displaystyle \boldsymbol f(n)=\sum_{d\mid n}(\boldsymbol f*\boldsymbol \mu)(d)\)
考虑回原来的题目:\(\text{gcd}(i,j)^k=\boldsymbol {id^k}[\gcd(i,j)]\)
\(\quad \displaystyle \sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)^k\)
\(=\displaystyle \sum_{i=1}^n\sum_{j=1}^m\sum_{d\mid i\wedge d\mid j}(\boldsymbol {id^k}*\boldsymbol \mu)(d)\)
\(=\displaystyle \sum_{d=1}^n(\boldsymbol {id^k}*\boldsymbol \mu)(d)\sum_{i=1}^n[d\mid i]\sum_{j=1}^m[d\mid j]\)
\(=\displaystyle \sum_{d=1}^n(\boldsymbol {id^k}*\boldsymbol \mu)(d)(n/d)(m/d)\)
形式已化简至同上式相同
推广
一般性地,考虑完全积性函数 \(\boldsymbol f_i,\boldsymbol f_j\) 与积性函数 \(\boldsymbol f\)
规定 \(n<m\) ,求和 \(\displaystyle \sum_{i=1}^n\sum_{j=1}^m\boldsymbol f_i(i)\boldsymbol f_j(j)\boldsymbol f[\gcd(i,j)]\)
直接考虑对 \(\boldsymbol f\) 进行反演
\(=\displaystyle \sum_{i=1}^n\sum_{j=1}^m\boldsymbol f_i(i)\boldsymbol f_j(j)\sum_{d\mid i\wedge d\mid j}(\boldsymbol f*\boldsymbol \mu)(d)\)
\(=\displaystyle \sum_{d=1}^n(\boldsymbol f*\boldsymbol \mu)(d)\sum_{i=1}^n\boldsymbol f_i(i)[d\mid i]\sum_{j=1}^m\boldsymbol f_j(j)[d\mid j]\)
\(=\displaystyle \sum_{d=1}^n(\boldsymbol f*\boldsymbol \mu\cdot \boldsymbol f_i\cdot \boldsymbol f_j)(d)\sum_{i=1}^{n/d}\boldsymbol f_i(i)\sum_{j=1}^{m/d}\boldsymbol f_j(j)\)
不妨设 \(\displaystyle \boldsymbol g=\boldsymbol f*\boldsymbol \mu\cdot \boldsymbol f_i\cdot \boldsymbol f_j,S_{fi}(n)=\sum_{i=1}^n\boldsymbol f_i(i),S_{fj}(n)=\sum_{j=1}^n\boldsymbol f_j(j)\)
默认函数间,狄利克雷卷积的运算优先级与点乘的优先级相同,两者从左向右计算
\(=\displaystyle \sum_{d=1}^n\boldsymbol g(d)S_{fi}(n/d)S_fj(m/d)\)
该式可线性筛求出 \(\boldsymbol g\) ,若对于 \(\boldsymbol f_i,\boldsymbol f_j\) 的前缀和较好处理(或可预处理),则可通过整除分块快速求出答案
\((\boldsymbol f*\boldsymbol \mu)(p^k)=\boldsymbol f(p^k)-\boldsymbol f(p^{k-1})\)
\(\boldsymbol g(p^k)=[\boldsymbol f(p^k)-\boldsymbol f(p^{k-1})]\cdot \boldsymbol f_i(p^k)\cdot \boldsymbol f_j(p^k)\)
再推广
更一般性地,考虑积性函数 \(\boldsymbol f\) , \(k\) 个完全积性函数 \(\boldsymbol f_k\) ,其自变量分别为 \(a_k\) ,求取对于区域 \([L_k,R_k]\) 的贡献 \((R_1\leq R_2\leq R_3\leq \cdots \leq R_k)\)
\(\quad \displaystyle \sum_{a_1=L_1}^{R_1}\boldsymbol f_1(a_1)\sum_{a_2=L_2}^{R_2}\boldsymbol f_2(a_2)\sum_{a_3=L_3}^{R_3}\boldsymbol f_3(a_3)\cdots \sum_{a_k=L_k}^{R_k}\boldsymbol f_k(a_k)\cdot \boldsymbol f[\gcd(a_1,a_2,a_3,\cdots ,a_k)]\)
\(=\displaystyle \sum_{d=1}^{R_1}(\boldsymbol f*\boldsymbol \mu)(d)\sum_{a_1=L_1}^{R_1}\boldsymbol f_1(a_1)[d\mid a_1]\sum_{a_2=L_2}^{R_2}\boldsymbol f_2(a_2)[d\mid a_2]\sum_{a_3=L_3}^{R_3}\boldsymbol f_3(a_3)[d\mid a_3]\cdots \sum_{a_k=L_k}^{R_k}\boldsymbol f_k(a_k)[d\mid a_k]\)
\(=\displaystyle \sum_{d=1}^{R_1}(\boldsymbol f*\boldsymbol \mu)(d)\sum_{a_1=\lceil{L_1\over d}\rceil}^{\lfloor{R_1\over d}\rfloor}\boldsymbol f_1(a_1)\boldsymbol f_1(d)\sum_{a_2=\lceil{L_2\over d}\rceil}^{\lfloor{R_2\over d}\rfloor}\boldsymbol f_2(a_2)\boldsymbol f_2(d)\sum_{a_3=\lceil{L_3\over d}\rceil}^{\lfloor{R_3\over d}\rfloor}\boldsymbol f_3(a_3)\boldsymbol f_3(d)\cdots \sum_{a_k=\lceil{L_k\over d}\rceil}^{\lfloor{R_k\over d}\rfloor}\boldsymbol f_k(a_k)\boldsymbol f_k(d)\)
记 \(\displaystyle \boldsymbol g=\boldsymbol f*\boldsymbol \mu\cdot (\prod_{i=1}^k\boldsymbol f_i)\)
\(=\displaystyle \sum_{d=1}^{R_1}\boldsymbol g(d)\cdot \prod_{j=1}^k[\sum_{a_j=\lceil{L_j\over d}\rceil}^{\lfloor{R_j\over d}\rfloor}\boldsymbol f_j(a_j)]\)
因为 \(\lceil{L\over d}\rceil=\lfloor{L-1\over d}\rfloor+1\)
故原式转化为
\(=\displaystyle \sum_{d=1}^{R_1}\boldsymbol g(d)\cdot \prod_{j=1}^k[\sum_{a_j=(L_j-1)/d+1}^{R_j/d}\boldsymbol f_j(a_j)]\)
\(=\displaystyle \sum_{d=1}^{R_1}\boldsymbol g(d)\cdot \prod_{j=1}^k[S_{fj}(R_j/d)-S_{fj}(\ (L_j-1)/d\ )]\)