约定:若有 n , m n, m n,m 两参数,默认 n ≤ m n \leq m n≤m
∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = prime ] \sum_{i=1}^n \sum_{j=1}^m [\gcd(i,j)=\text{prime}] i=1∑nj=1∑m[gcd(i,j)=prime]
= ∑ p ∈ prime ∑ i = 1 ⌊ n p ⌋ ∑ j = 1 ⌊ m p ⌋ [ gcd ( i , j ) = 1 ] =\sum_{p \in \text{prime}} \sum_{i=1}^{\lfloor \frac{n}{p} \rfloor} \sum_{j=1}^{\lfloor \frac{m}{p} \rfloor} [\gcd(i,j)=1] =p∈prime∑i=1∑⌊pn⌋j=1∑⌊pm⌋[gcd(i,j)=1]
= ∑ p ∈ prime ∑ i = 1 ⌊ n p ⌋ ∑ j = 1 ⌊ m p ⌋ ∑ d ∣ gcd ( i , j ) μ ( d ) =\sum_{p \in \text{prime}} \sum_{i=1}^{\lfloor \frac{n}{p} \rfloor} \sum_{j=1}^{\lfloor \frac{m}{p} \rfloor} \sum_{d|\gcd(i,j)} \mu(d) =p∈prime∑i=1∑⌊pn⌋j=1∑⌊pm⌋d∣gcd(i,j)∑μ(d)
= ∑ p ∈ prime ∑ i = 1 m i n ( ⌊ n p ⌋ , ⌊ n p ⌋ ) μ ( i ) ⌊ n p i ⌋ ⌊ m p i ⌋ =\sum_{p \in \text{prime}} \sum_{i=1}^{min(\lfloor \frac{n}{p} \rfloor,\lfloor \frac{n}{p} \rfloor)} \mu(i) \lfloor \frac{n}{pi} \rfloor \lfloor \frac{m}{pi} \rfloor =p∈prime∑i=1∑min(⌊pn⌋,⌊pn⌋)μ(i)⌊pin⌋⌊pim⌋
= ∑ i = 1 m i n ( n , m ) ⌊ n i ⌋ ⌊ m i ⌋ ∑ p ∈ prime , p ∣ i μ ( i p ) =\sum_{i=1}^{min(n,m)} \lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{i} \rfloor \sum_{p \in \text{prime},p|i} \mu(\dfrac{i}{p}) =i=1∑min(n,m)⌊in⌋⌊im⌋p∈prime,p∣i∑μ(pi)
预处理 ∑ p ∈ prime , p ∣ i μ ( i p ) \sum_{p \in \text{prime},p|i} \mu(\dfrac{i}{p}) ∑p∈prime,p∣iμ(pi) 即可
∑ i = 1 n ∑ j = 1 m 2 gcd ( i , j ) − 1 \sum_{i=1}^n \sum_{j=1}^m 2 \gcd(i,j) -1 i=1∑nj=1∑m2gcd(i,j)−1
= ( 2 ∑ i = 1 n ∑ j = 1 m gcd ( i , j ) ) − n m = \left( 2 \sum_{i=1}^n \sum_{j=1}^m \gcd(i,j) \right)- nm =(2i=1∑nj=1∑mgcd(i,j))−nm
= ( 2 ∑ i = 1 n ∑ x = 1 ⌊ n i ⌋ ∑ y = 1 ⌊ m i ⌋ i [ gcd ( x , y ) = 1 ] ) − n m = \left( 2 \sum_{i=1}^n \sum_{x=1}^{\lfloor \frac{n}{i} \rfloor} \sum_{y=1}^{\lfloor \frac{m}{i} \rfloor} i[\gcd(x,y)=1] \right) - nm =⎝⎛2i=1∑nx=1∑⌊in⌋y=1∑⌊im⌋i[gcd(x,y)=1]⎠⎞−nm
= ( 2 ∑ i = 1 n ∑ x = 1 ⌊ n i ⌋ ∑ y = 1 ⌊ m i ⌋ i ∑ d ∣ gcd ( x , y ) μ ( d ) ) − n m = \left( 2 \sum_{i=1}^n \sum_{x=1}^{\lfloor \frac{n}{i} \rfloor} \sum_{y=1}^{\lfloor \frac{m}{i} \rfloor} i \sum_{d|\gcd(x,y)} \mu(d) \right) - nm =⎝⎛2i=1∑nx=1∑⌊in⌋y=1∑⌊im⌋id∣gcd(x,y)∑μ(d)⎠⎞−nm
= ( 2 ∑ i = 1 n ∑ j = 1 ⌊ n i ⌋ μ ( j ) ⌊ n i j ⌋ ⌊ m i j ⌋ i ) − n m = \left( 2 \sum_{i=1}^n \sum_{j=1}^{\lfloor \frac{n}{i} \rfloor} \mu(j) \lfloor \frac{n}{ij} \rfloor \lfloor \frac{m}{ij} \rfloor i \right) - nm =⎝⎛2i=1∑nj=1∑⌊in⌋μ(j)⌊ijn⌋⌊ijm⌋i⎠⎞−nm
= ( 2 ∑ i = 1 n ⌊ n i ⌋ ⌊ m i ⌋ ∑ d ∣ i d μ ( i d ) ) − n m = \left( 2 \sum_{i=1}^n \lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{i} \rfloor \sum_{d|i} d \mu(\dfrac{i}{d}) \right) - nm =⎝⎛2i=1∑n⌊in⌋⌊im⌋d∣i∑dμ(di)⎠⎞−nm
可以预处理 ∑ d ∣ i d μ ( i d ) \sum_{d|i} d \mu(\dfrac{i}{d}) ∑d∣idμ(di) 的前缀和
∑ i = 1 n ∑ j = 1 m ∑ d ∣ gcd ( i , j ) d [ ∑ d ∣ gcd ( i , j ) d ≤ a ] \sum_{i=1}^n \sum_{j=1}^m \sum_{d|\gcd(i,j)} d [\sum_{d|\gcd(i,j)} d \leq a] i=1∑nj=1∑md∣gcd(i,j)∑d[d∣gcd(i,j)∑d≤a]
= ∑ i = 1 n σ 1 ( i ) [ σ 1 ( i ) ≤ a ] ∑ x = 1 ⌊ n i ⌋ ∑ y = 1 ⌊ m i ⌋ [ g c d ( x , y ) = 1 ] = \sum_{i=1}^n \sigma_1(i) [\sigma_1(i) \leq a] \sum_{x=1}^{\lfloor \frac{n}{i} \rfloor} \sum_{y=1}^{\lfloor \frac{m}{i} \rfloor} [gcd(x,y)=1] =i=1∑nσ1(i)[σ1(i)≤a]x=1∑⌊in⌋y=1∑⌊im⌋[gcd(x,y)=1]
= ∑ i = 1 n σ 1 ( i ) [ σ 1 ( i ) ≤ a ] ∑ x = 1 ⌊ n i ⌋ ∑ y = 1 ⌊ m i ⌋ ∑ d ∣ g c d ( x , y ) μ ( d ) = \sum_{i=1}^n \sigma_1(i) [\sigma_1(i) \leq a] \sum_{x=1}^{\lfloor \frac{n}{i} \rfloor} \sum_{y=1}^{\lfloor \frac{m}{i} \rfloor} \sum_{d|gcd(x,y)} \mu(d) =i=1∑nσ1(i)[σ1(i)≤a]x=1∑⌊in⌋y=1∑⌊im⌋d∣gcd(x,y)∑μ(d)
= ∑ i = 1 n σ 1 ( i ) [ σ 1 ( i ) ≤ a ] ∑ j = 1 ⌊ n i ⌋ μ ( j ) ⌊ n i j ⌋ ⌊ m i j ⌋ = \sum_{i=1}^n \sigma_1(i) [\sigma_1(i) \leq a] \sum_{j=1}^{\lfloor \frac{n}{i} \rfloor} \mu(j) \lfloor \frac{n}{ij} \rfloor \lfloor \frac{m}{ij} \rfloor =i=1∑nσ1(i)[σ1(i)≤a]j=1∑⌊in⌋μ(j)⌊ijn⌋⌊ijm⌋
= ∑ i = 1 n ⌊ n i ⌋ ⌊ m i ⌋ ∑ d ∣ i μ ( i d ) σ 1 ( d ) [ σ 1 ( d ) ≤ a ] = \sum_{i=1}^n \lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{i} \rfloor \sum_{d|i} \mu(\dfrac{i}{d}) \sigma_1(d)[\sigma_1(d) \leq a] =i=1∑n⌊in⌋⌊im⌋d∣i∑μ(di)σ1(d)[σ1(d)≤a]
离线预处理 ∑ d ∣ i μ ( i d ) σ 1 ( d ) [ σ 1 ( d ) ≤ a ] \sum_{d|i} \mu(\dfrac{i}{d}) \sigma_1(d)[\sigma_1(d) \leq a] ∑d∣iμ(di)σ1(d)[σ1(d)≤a] 即可
∏ i = 1 n ∏ j = 1 m f [ gcd ( i , j ) ] \prod_{i=1}^n \prod_{j=1}^m f[\gcd(i,j)] i=1∏nj=1∏mf[gcd(i,j)]
= ∏ i = 1 n f [ i ] ∑ x = 1 ⌊ n i ⌋ ∑ y = 1 ⌊ m i ⌋ [ g c d ( i , j ) = 1 ] =\prod_{i=1}^n f[i]^{\sum_{x=1}^{\lfloor \frac{n}{i} \rfloor} \sum_{y=1}^{\lfloor \frac{m}{i} \rfloor} [gcd(i,j)=1]} =i=1∏nf[i]∑x=1⌊in⌋∑y=1⌊im⌋[gcd(i,j)=1]
= ∏ i = 1 n f [ i ] ∑ j = 1 ⌊ n i ⌋ μ ( j ) ⌊ n i j ⌋ ⌊ m i j ⌋ =\prod_{i=1}^n f[i]^{\sum_{j=1}^{\lfloor \frac{n}{i} \rfloor} \mu(j) \lfloor \frac{n}{ij} \rfloor \lfloor \frac{m}{ij} \rfloor } =i=1∏nf[i]∑j=1⌊in⌋μ(j)⌊ijn⌋⌊ijm⌋
= ∏ i = 1 n ∏ j = 1 ⌊ n i ⌋ f [ i ] μ ( j ) ⌊ n i j ⌋ ⌊ m i j ⌋ =\prod_{i=1}^n \prod_{j=1}^{\lfloor \frac{n}{i} \rfloor} f[i]^{\mu(j) \lfloor \frac{n}{ij} \rfloor \lfloor \frac{m}{ij} \rfloor} =i=1∏nj=1∏⌊in⌋f[i]μ(j)⌊ijn⌋⌊ijm⌋
= ∏ i = 1 n ( ∏ d ∣ i f [ d ] μ ( i d ) ) ⌊ n i ⌋ ⌊ m i ⌋ =\prod_{i=1}^n (\prod_{d|i}f[d]^{\mu(\frac{i}{d})})^{\lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{i} \rfloor} =i=1∏n(d∣i∏f[d]μ(di))⌊in⌋⌊im⌋
预处理 ∏ d ∣ i f [ d ] μ ( i d ) \prod_{d|i}f[d]^{\mu(\frac{i}{d})} ∏d∣if[d]μ(di) 的前缀积即可
∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = d ] \sum_{i=1}^n \sum_{j=1}^m [\gcd(i,j)=d] i=1∑nj=1∑m[gcd(i,j)=d]
= ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ [ gcd ( i , j ) = 1 ] =\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j=1}^{\lfloor \frac{m}{d} \rfloor} [\gcd(i,j)=1] =i=1∑⌊dn⌋j=1∑⌊dm⌋[gcd(i,j)=1]
= ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ ∑ k ∣ gcd ( i , j ) μ ( k ) =\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j=1}^{\lfloor \frac{m}{d} \rfloor} \sum_{k|\gcd(i,j)} \mu(k) =i=1∑⌊dn⌋j=1∑⌊dm⌋k∣gcd(i,j)∑μ(k)
= ∑ i = 1 ⌊ n d ⌋ μ ( i ) ⌊ n d i ⌋ ⌊ m d i ⌋ =\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor} \mu(i) \lfloor \frac{n}{di} \rfloor \lfloor \frac{m}{di} \rfloor =i=1∑⌊dn⌋μ(i)⌊din⌋⌊dim⌋
∑ i = 1 b ∑ j = 1 d [ gcd ( i , j ) = k ] − ∑ i = 1 a ∑ j = 1 d [ gcd ( i , j ) = k ] − ∑ i = 1 b ∑ j = 1 c [ gcd ( i , j ) = k ] + ∑ i = 1 a ∑ j = 1 c [ gcd ( i , j ) = k ] \sum_{i=1}^b \sum_{j=1}^d [\gcd(i,j)=k] - \sum_{i=1}^a \sum_{j=1}^d [\gcd(i,j)=k] - \sum_{i=1}^b \sum_{j=1}^c [\gcd(i,j)=k] + \sum_{i=1}^a \sum_{j=1}^c [\gcd(i,j)=k] i=1∑bj=1∑d[gcd(i,j)=k]−i=1∑aj=1∑d[gcd(i,j)=k]−i=1∑bj=1∑c[gcd(i,j)=k]+i=1∑aj=1∑c[gcd(i,j)=k]
同上即可
∑ i = 1 n ∑ j = 1 m σ 0 ( i j ) \sum_{i=1}^n \sum_{j=1}^m \sigma_0(ij) i=1∑nj=1∑mσ0(ij)
= ∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j [ gcd ( x , y ) = 1 ] =\sum_{i=1}^n \sum_{j=1}^m \sum_{x|i} \sum_{y|j} [\gcd(x,y)=1] =i=1∑nj=1∑mx∣i∑y∣j∑[gcd(x,y)=1]
= ∑ x = 1 n ∑ y = 1 m ∑ i = 1 ⌊ n x ⌋ ∑ j = 1 ⌊ m y ⌋ [ gcd ( x , y ) = 1 ] =\sum_{x=1}^n \sum_{y=1}^m \sum_{i=1}^{\lfloor \frac{n}{x} \rfloor} \sum_{j=1}^{\lfloor \frac{m}{y} \rfloor} [\gcd(x,y)=1] =x=1∑ny=1∑mi=1∑⌊xn⌋j=1∑⌊ym⌋[gcd(x,y)=1]
= ∑ x = 1 n ∑ y = 1 m ⌊ n x ⌋ ⌊ m y ⌋ [ gcd ( x , y ) = 1 ] =\sum_{x=1}^n \sum_{y=1}^m \lfloor \frac{n}{x} \rfloor \lfloor \frac{m}{y} \rfloor [\gcd(x,y)=1] =x=1∑ny=1∑m⌊xn⌋⌊ym⌋[gcd(x,y)=1]
= ∑ x = 1 n ∑ y = 1 m ⌊ n x ⌋ ⌊ m y ⌋ ∑ d ∣ g c d ( x , y ) μ ( d ) =\sum_{x=1}^n \sum_{y=1}^m \lfloor \frac{n}{x} \rfloor \lfloor \frac{m}{y} \rfloor \sum_{d|gcd(x,y)} \mu(d) =x=1∑ny=1∑m⌊xn⌋⌊ym⌋d∣gcd(x,y)∑μ(d)
= ∑ d = 1 n μ ( d ) ∑ x = 1 ⌊ n d ⌋ ⌊ n x d ⌋ ∑ y = 1 ⌊ m d ⌋ ⌊ m y d ⌋ =\sum_{d=1}^n \mu(d) \sum_{x=1}^{\lfloor \frac{n}{d} \rfloor} \lfloor \frac{n}{xd} \rfloor \sum_{y=1}^{\lfloor \frac{m}{d} \rfloor} \lfloor \frac{m}{yd} \rfloor =d=1∑nμ(d)x=1∑⌊dn⌋⌊xdn⌋y=1∑⌊dm⌋⌊ydm⌋
= ∑ d = 1 n μ ( d ) d i v s ( ⌊ n d ⌋ ) d i v s ( ⌊ m d ⌋ ) =\sum_{d=1}^n \mu(d) divs(\lfloor \frac{n}{d} \rfloor) divs(\lfloor \frac{m}{d} \rfloor) =d=1∑nμ(d)divs(⌊dn⌋)divs(⌊dm⌋)
∑ x 1 = l h ∑ x 2 = l h ⋯ ∑ x n = l h [ gcd ( x 1 , x 2 , ⋯ , x n ) = k ] \sum_{x_1=l}^h \sum_{x_2=l}^h \cdots \sum_{x_n=l}^h [\gcd(x_1,x_2,\cdots,x_n)=k] x1=l∑hx2=l∑h⋯xn=l∑h[gcd(x1,x2,⋯,xn)=k]
= ∑ x 1 = ⌈ l k ⌉ ⌊ h k ⌋ ∑ x 2 = ⌈ l k ⌉ ⌊ h k ⌋ ⋯ ∑ x n = ⌈ l k ⌉ ⌊ h k ⌋ ∑ d ∣ gcd ( x 1 , x 2 , ⋯ , x n ) μ ( d ) =\sum_{x_1=\lceil \frac{l}{k} \rceil}^{\lfloor \frac{h}{k} \rfloor} \sum_{x_2=\lceil \frac{l}{k} \rceil}^{\lfloor \frac{h}{k} \rfloor} \cdots \sum_{x_n=\lceil \frac{l}{k} \rceil}^{\lfloor \frac{h}{k} \rfloor} \sum_{d|\gcd(x_1,x_2,\cdots,x_n)} \mu(d) =x1=⌈kl⌉∑⌊kh⌋x2=⌈kl⌉∑⌊kh⌋⋯xn=⌈kl⌉∑⌊kh⌋d∣gcd(x1,x2,⋯,xn)∑μ(d)
= ∑ d = ⌈ l k ⌉ ⌊ h k ⌋ μ ( d ) ( ⌊ h k d ⌋ − ⌈ ⌈ l k ⌉ d ⌉ ) n =\sum_{d=\lceil \frac{l}{k} \rceil}^{\lfloor \frac{h}{k} \rfloor} \mu(d) (\lfloor \frac{h}{kd} \rfloor - \lceil \frac{\lceil \frac{l}{k} \rceil}{d} \rceil)^n =d=⌈kl⌉∑⌊kh⌋μ(d)(⌊kdh⌋−⌈d⌈kl⌉⌉)n
ϵ ( i ) = [ i = 1 ] \epsilon(i)=[i=1] ϵ(i)=[i=1]
I ( i ) = 1 \text{I}(i)=1 I(i)=1
id ( i ) = i \text{id}(i)=i id(i)=i
μ ∗ I = ϵ \mu * \text{I} =\epsilon μ∗I=ϵ
μ ∗ id = φ \mu * \text{id} =\varphi μ∗id=φ
φ ∗ I = id \varphi * \text{I} = \text{id} φ∗I=id
∑ i = 1 n ∑ j = 1 n i j gcd ( i , j ) \sum_{i=1}^n \sum_{j=1}^n ij\gcd(i,j) i=1∑nj=1∑nijgcd(i,j)
= ∑ i = 1 n ∑ x = 1 ⌊ n i ⌋ ∑ y = 1 ⌊ n i ⌋ x y i 3 [ gcd ( x , y ) = = 1 ] =\sum_{i=1}^n \sum_{x=1}^{\lfloor \frac{n}{i} \rfloor} \sum_{y=1}^{\lfloor \frac{n}{i} \rfloor}xyi^3[\gcd(x,y)==1] =i=1∑nx=1∑⌊in⌋y=1∑⌊in⌋xyi3[gcd(x,y)==1]
= ∑ i = 1 n ∑ x = 1 ⌊ n i ⌋ ∑ y = 1 ⌊ n i ⌋ x y i 3 ∑ d ∣ gcd ( x , y ) μ ( d ) =\sum_{i=1}^n \sum_{x=1}^{\lfloor \frac{n}{i} \rfloor} \sum_{y=1}^{\lfloor \frac{n}{i} \rfloor}xyi^3 \sum_{d|\gcd(x,y)} \mu(d) =i=1∑nx=1∑⌊in⌋y=1∑⌊in⌋xyi3d∣gcd(x,y)∑μ(d)
= ∑ i = 1 n i 3 ∑ j = 1 ⌊ n i ⌋ j 2 μ ( j ) ∑ x = 1 ⌊ n i j ⌋ x ∑ y = 1 ⌊ n i j ⌋ y =\sum_{i=1}^n i^3 \sum_{j=1}^{\lfloor \frac{n}{i} \rfloor} j^2 \mu(j) \sum_{x=1}^{\lfloor \frac{n}{ij} \rfloor} x \sum_{y=1}^{\lfloor \frac{n}{ij} \rfloor} y =i=1∑ni3j=1∑⌊in⌋j2μ(j)x=1∑⌊ijn⌋xy=1∑⌊ijn⌋y
= ∑ i = 1 n i 3 ∑ j = 1 ⌊ n i ⌋ j 2 μ ( j ) ( ⌊ n i j ⌋ ) 2 ( ⌊ n i j ⌋ + 1 ) 2 4 =\sum_{i=1}^n i^3 \sum_{j=1}^{\lfloor \frac{n}{i} \rfloor} j^2 \mu(j) \frac{(\lfloor \frac{n}{ij} \rfloor)^2(\lfloor \frac{n}{ij} \rfloor + 1)^2}{4} =i=1∑ni3j=1∑⌊in⌋j2μ(j)4(⌊ijn⌋)2(⌊ijn⌋+1)2
= ∑ i = 1 n i 2 ( ⌊ n i ⌋ ) 2 ( ⌊ n i ⌋ + 1 ) 2 4 ∑ d ∣ i d μ ( i d ) =\sum_{i=1}^n i^2 \frac{(\lfloor \frac{n}{i} \rfloor)^2(\lfloor \frac{n}{i} \rfloor + 1)^2}{4} \sum_{d|i} d \mu(\frac{i}{d}) =i=1∑ni24(⌊in⌋)2(⌊in⌋+1)2d∣i∑dμ(di)
= ∑ i = 1 n i 2 φ ( i ) ( ⌊ n i ⌋ ) 2 ( ⌊ n i ⌋ + 1 ) 2 4 =\sum_{i=1}^n i^2 \varphi(i)\frac{(\lfloor \frac{n}{i} \rfloor)^2(\lfloor \frac{n}{i} \rfloor + 1)^2}{4} =i=1∑ni2φ(i)4(⌊in⌋)2(⌊in⌋+1)2
杜教筛 i 2 φ ( i ) i^2 \varphi(i) i2φ(i) 前缀和,整除分块
如何杜教筛 i 2 φ ( i ) i^2 \varphi(i) i2φ(i):
f ( i ) = i 2 φ ( i ) f(i)=i^2 \varphi(i) f(i)=i2φ(i)
g ( i ) = id ( i 2 ) g(i)=\text{id}(i^2) g(i)=id(i2)
( f ∗ g ) ( i ) = ∑ d ∣ i ( d 2 φ ( d ) ) ( i d ) 2 = i 2 ∑ d ∣ i φ ( d ) = i 3 (f * g)(i)= \sum_{d|i} (d^2 \varphi(d))(\frac{i}{d})^2 = i^2 \sum_{d|i} \varphi(d) = i^3 (f∗g)(i)=d∣i∑(d2φ(d))(di)2=i2d∣i∑φ(d)=i3