bzoj 3994: [SDOI2015]约数个数和

题目描述

给你 N , M N,M N,M,求 ∑ i = 1 N ∑ j = 1 M d ( i j ) \sum_{i=1}^{N}\sum_{j=1}^{M}d(ij) i=1Nj=1Md(ij)你需要回答 T T T 个询问。 1 ≤ N , M , T ≤ 50   000 1\leq N,M,T\leq50\ 000 1N,M,T50 000

Solution

今有结论 (*) d ( i j ) = ∑ x ∣ i ∑ y ∣ j [ gcd ⁡ ( x , y ) = 1 ] d(ij)=\sum_{x|i}\sum_{y|j}[\gcd(x,y)=1]\tag{*} d(ij)=xiyj[gcd(x,y)=1](*)至于为什么,以后再写。

∑ i = 1 N ∑ j = 1 M d ( i j ) = ∑ i = 1 N ∑ j = 1 M ∑ x ∣ i ∑ y ∣ j [ gcd ⁡ ( x , y ) = 1 ] = ∑ i = 1 N ∑ j = 1 M ∑ x ∣ i ∑ y ∣ j ∑ d ∣ gcd ⁡ ( x , y ) μ ( d ) = ∑ d = 1 min ⁡ ( N , M ) ( ∑ d ∣ x x ≤ N ∑ x ∣ i i ≤ N 1 ) ( ∑ d ∣ y y ≤ M ∑ y ∣ j j ≤ M 1 ) = ∑ d = 1 min ⁡ ( N , M ) ( ∑ d ∣ x x ≤ N ⌊ N x ⌋ ) ( ∑ d ∣ y y ≤ M ⌊ M y ⌋ ) \begin{aligned}\sum_{i=1}^N\sum_{j=1}^Md(ij)&=\sum_{i=1}^N\sum_{j=1}^M\sum_{x|i}\sum_{y|j}[\gcd(x,y)=1]\\ &=\sum_{i=1}^N\sum_{j=1}^M\sum_{x|i}\sum_{y|j}\sum_{d|\gcd(x,y)}\mu(d)\\ &=\sum_{d=1}^{\min(N,M)}(\sum_{d|x}^{x\leq N}\sum_{x|i}^{i\leq N}1)(\sum_{d|y}^{y\leq M}\sum_{y|j}^{j\leq M}1)\\ &=\sum_{d=1}^{\min(N,M)}(\sum_{d|x}^{x\leq N}\lfloor\frac Nx\rfloor)(\sum_{d|y}^{y\leq M}\lfloor\frac My\rfloor)\end{aligned} i=1Nj=1Md(ij)=i=1Nj=1Mxiyj[gcd(x,y)=1]=i=1Nj=1Mxiyjdgcd(x,y)μ(d)=d=1min(N,M)(dxxNxiiN1)(dyyMyjjM1)=d=1min(N,M)(dxxNxN)(dyyMyM)
O ( n ) O(n) O(n) 预处理后面括号里的内容。

你可能感兴趣的:(信息学,数学,Mobius,反演与杜教筛)