最大公约数 : Greatest Common Divisor, 简称 : gcd
定义符号 : d|n d | n : n%d=0 n % d = 0
定义函数 : φ(n) φ ( n ) : 欧拉函数
定义函数 : μ(n) μ ( n ) : 莫比乌斯函数
一维形式: ∑i=1ngcd(n,i) ∑ i = 1 n g c d ( n , i )
1. 1≤n≤106 1 ≤ n ≤ 10 6
相信大家都会
for(int i=1;i<=n;++i)
ans+=gcd(n,i);
2. 1≤n≤109 1 ≤ n ≤ 10 9
设: gcd(n,i)=d g c d ( n , i ) = d
显然: n%d=0 n % d = 0
原问题转换为:
∑d|nd⋅∑i=1n[gcd(n,i)=d] ∑ d | n d ⋅ ∑ i = 1 n [ g c d ( n , i ) = d ]
∑d|nd⋅∑i=1nd[gcd(nd,i)=1] ∑ d | n d ⋅ ∑ i = 1 n d [ g c d ( n d , i ) = 1 ]
∑d|nd⋅φ(nd) ∑ d | n d ⋅ φ ( n d )
O(n−−√) O ( n ) 枚举 n n 的因子, 再 O(nd−−√) O ( n d ) 计算 φ(nd) φ ( n d ) 就好了
3. 1≤n≤1012 1 ≤ n ≤ 10 12
不难发现 ∑d|nd⋅φ(nd) ∑ d | n d ⋅ φ ( n d ) 是狄利克雷卷积形式
f(n)=n f ( n ) = n 与 φ(n) φ ( n ) 均为积性函数
所以 g(n)=∑d|nd⋅φ(nd) g ( n ) = ∑ d | n d ⋅ φ ( n d ) 为积性函数
若: n=pe11⋅pe22⋯pekkp1,p2,⋯,pk n = p 1 e 1 ⋅ p 2 e 2 ⋯ p k e k p 1 , p 2 , ⋯ , p k 均为质数
那么: g(n)=g(pe11)⋅g(pe22)×⋯×g(pekk) g ( n ) = g ( p 1 e 1 ) ⋅ g ( p 2 e 2 ) × ⋯ × g ( p k e k )
考虑: g(pk)=∑d|pkd⋅φ(pkd)p g ( p k ) = ∑ d | p k d ⋅ φ ( p k d ) p 为质数
即: g(pk)=∑i=0kpk−i⋅φ(pi)=∑i=1kpk−i⋅φ(pi)+pk⋅φ(1) g ( p k ) = ∑ i = 0 k p k − i ⋅ φ ( p i ) = ∑ i = 1 k p k − i ⋅ φ ( p i ) + p k ⋅ φ ( 1 )
g(pk)=pk+∑i=1kpk−i⋅(pi−pi−1)=pk+∑i=1k(pk−pk−1) g ( p k ) = p k + ∑ i = 1 k p k − i ⋅ ( p i − p i − 1 ) = p k + ∑ i = 1 k ( p k − p k − 1 )
g(pk)=(k+1)⋅pk−k⋅pk−1 g ( p k ) = ( k + 1 ) ⋅ p k − k ⋅ p k − 1
复杂度: O(n−−√) O ( n )
二维形式: ∑i=1n∑j=1mgcd(i,j) ∑ i = 1 n ∑ j = 1 m g c d ( i , j )
1. 1≤n,m≤103 1 ≤ n , m ≤ 10 3
相信大家都会
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
ans+=gcd(i,j);
2. 1≤n=m≤105q 1 ≤ n = m ≤ 10 5 q 组询问
设: g(n)=∑i=1ngcd(n,i) g ( n ) = ∑ i = 1 n g c d ( n , i )
我们已经可以在 O(n−−√) O ( n ) 内得到 g(n) g ( n )
∑i=1n∑j=1ngcd(i,j)=2⋅∑i=1n∑j=1igcd(i,j)−∑i=1ngcd(i,i)=2⋅∑i=1n∑j=1igcd(i,j)−(n+1)⋅n2=2⋅∑i=1ng(i)−(n+1)⋅n2(8)(9)(10) (8) ∑ i = 1 n ∑ j = 1 n g c d ( i , j ) = 2 ⋅ ∑ i = 1 n ∑ j = 1 i g c d ( i , j ) − ∑ i = 1 n g c d ( i , i ) (9) = 2 ⋅ ∑ i = 1 n ∑ j = 1 i g c d ( i , j ) − ( n + 1 ) ⋅ n 2 (10) = 2 ⋅ ∑ i = 1 n g ( i ) − ( n + 1 ) ⋅ n 2
复杂度: O(nn−−√+q) O ( n n + q )
3. 1≤n=m≤107q 1 ≤ n = m ≤ 10 7 q 组询问
枚举: gcd(i,j)=d∈[1,n] g c d ( i , j ) = d ∈ [ 1 , n ]
原问题转换为:
∑d=1nd∑i=1n∑j=1n[gcd(i,j)=d] ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = d ]
∑d=1nd∑i=1⌊nd⌋∑j=1⌊nd⌋[gcd(i,j)=1] ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ [ g c d ( i , j ) = 1 ]
设: f(n)=∑i=1n∑j=1n[gcd(i,j)=1] f ( n ) = ∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = 1 ]
设: g(n)=∑i=1n∑j=1i[gcd(i,j)=1]=∑i=1nφ(i) g ( n ) = ∑ i = 1 n ∑ j = 1 i [ g c d ( i , j ) = 1 ] = ∑ i = 1 n φ ( i )
不难发现: f(n)=2⋅g(n)−1 f ( n ) = 2 ⋅ g ( n ) − 1
∑d=1nd∑i=1⌊nd⌋∑j=1⌊nd⌋[gcd(i,j)=1]=∑d=1nd⋅f(⌊nd⌋)=∑d=1nd⋅(2⋅g(⌊nd⌋)−1) ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ [ g c d ( i , j ) = 1 ] = ∑ d = 1 n d ⋅ f ( ⌊ n d ⌋ ) = ∑ d = 1 n d ⋅ ( 2 ⋅ g ( ⌊ n d ⌋ ) − 1 )
显然: ∑d=1nd⋅(2⋅g(⌊nd⌋)−1) ∑ d = 1 n d ⋅ ( 2 ⋅ g ( ⌊ n d ⌋ ) − 1 ) 可以分块
复杂度: O(n+q⋅n−−√) O ( n + q ⋅ n )
∑i=1n∑j=1ngcd(i,j)=2⋅∑i=1n∑j=1igcd(i,j)−(n+1)⋅n2 ∑ i = 1 n ∑ j = 1 n g c d ( i , j ) = 2 ⋅ ∑ i = 1 n ∑ j = 1 i g c d ( i , j ) − ( n + 1 ) ⋅ n 2
设: g(n)=∑i=1n∑j=1igcd(i,j)=∑i=1n∑d|id⋅φ(id) g ( n ) = ∑ i = 1 n ∑ j = 1 i g c d ( i , j ) = ∑ i = 1 n ∑ d | i d ⋅ φ ( i d )
∑d|nd⋅φ(nd) ∑ d | n d ⋅ φ ( n d ) 是积性函数, 可以线性筛, 同时维护它的前缀和就可以 O(1) O ( 1 ) 回答询问
复杂度: O(n+q) O ( n + q )
4. 1≤n≤m≤105 1 ≤ n ≤ m ≤ 10 5
枚举: gcd(i,j)=d∈[1,n] g c d ( i , j ) = d ∈ [ 1 , n ]
原问题转换为:
∑d=1nd∑i=1n∑j=1m[gcd(i,j)=d] ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = d ]
设: g(x)=∑i=1n∑j=1m[gcd(i,j)=x] g ( x ) = ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = x ]
设: f(x)=∑i=1n∑j=1m[x∣∣gcd(i,j)]=⌊nx⌋⋅⌊mx⌋ f ( x ) = ∑ i = 1 n ∑ j = 1 m [ x | g c d ( i , j ) ] = ⌊ n x ⌋ ⋅ ⌊ m x ⌋
莫比乌斯反演: f(x)=∑x|dg(d)⇒g(x)=∑x|dμ(dx)f(d) f ( x ) = ∑ x | d g ( d ) ⇒ g ( x ) = ∑ x | d μ ( d x ) f ( d )
g(x)=∑x|dμ(dx)⋅⌊nd⌋⋅⌊md⌋ g ( x ) = ∑ x | d μ ( d x ) ⋅ ⌊ n d ⌋ ⋅ ⌊ m d ⌋
∑d=1nd∑d|iμ(id)⋅⌊ni⌋⋅⌊mi⌋ ∑ d = 1 n d ∑ d | i μ ( i d ) ⋅ ⌊ n i ⌋ ⋅ ⌊ m i ⌋
复杂度: O(nlogn) O ( n l o g n )
5. 1≤n≤m≤106 1 ≤ n ≤ m ≤ 10 6
∑d=1nd∑i=1n∑j=1m[gcd(i,j)=d]⇒∑d=1nd∑i=1⌊nd⌋∑j=1⌊md⌋[gcd(i,j)=1] ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = d ] ⇒ ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ [ g c d ( i , j ) = 1 ]
莫比乌斯反演得: ∑i=1⌊nd⌋∑j=1⌊md⌋[gcd(i,j)=1]=∑i=1⌊nd⌋μ(i)⋅⌊⌊nd⌋i⌋⋅⌊⌊md⌋i⌋ ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ [ g c d ( i , j ) = 1 ] = ∑ i = 1 ⌊ n d ⌋ μ ( i ) ⋅ ⌊ ⌊ n d ⌋ i ⌋ ⋅ ⌊ ⌊ m d ⌋ i ⌋
得: ∑d=1nd∑i=1⌊nd⌋μ(i)⋅⌊⌊nd⌋i⌋⋅⌊⌊md⌋i⌋ ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ μ ( i ) ⋅ ⌊ ⌊ n d ⌋ i ⌋ ⋅ ⌊ ⌊ m d ⌋ i ⌋
令: f(x,y)=∑i=1xμ(i)⋅⌊xi⌋⋅⌊yi⌋ f ( x , y ) = ∑ i = 1 x μ ( i ) ⋅ ⌊ x i ⌋ ⋅ ⌊ y i ⌋
利用数论分块可以在 O(x−−√+y√) O ( x + y ) 得到函数值
同样的, 已知 f(x,y) f ( x , y ) 的值, ∑d=1nd⋅f(⌊nd⌋,⌊md⌋) ∑ d = 1 n d ⋅ f ( ⌊ n d ⌋ , ⌊ m d ⌋ ) 可以在 O(n−−√+m−−√) O ( n + m ) 内得到答案
所以可以分块套分块, 复杂度: O((n−−√+m−−√)2) O ( ( n + m ) 2 )
6. 1≤n≤m≤107q 1 ≤ n ≤ m ≤ 10 7 q 组询问
∑d=1nd∑i=1⌊nd⌋μ(i)⋅⌊⌊nd⌋i⌋⋅⌊⌊md⌋i⌋=∑d=1nd∑i=1⌊nd⌋μ(i)⋅⌊ni⋅d⌋⋅⌊mi⋅d⌋ ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ μ ( i ) ⋅ ⌊ ⌊ n d ⌋ i ⌋ ⋅ ⌊ ⌊ m d ⌋ i ⌋ = ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ μ ( i ) ⋅ ⌊ n i ⋅ d ⌋ ⋅ ⌊ m i ⋅ d ⌋
令: k=i⋅dk∈[1,n] k = i ⋅ d k ∈ [ 1 , n ]
枚举 k k : ∑k=1n∑d|kd⋅μ(kd)⋅⌊nk⌋⋅⌊mk⌋=∑k=1n⌊nk⌋⋅⌊mk⌋∑d|kd⋅μ(kd) ∑ k = 1 n ∑ d | k d ⋅ μ ( k d ) ⋅ ⌊ n k ⌋ ⋅ ⌊ m k ⌋ = ∑ k = 1 n ⌊ n k ⌋ ⋅ ⌊ m k ⌋ ∑ d | k d ⋅ μ ( k d )
不难发现: ∑d|kd⋅μ(kd) ∑ d | k d ⋅ μ ( k d ) 是狄利克雷卷积形式
f(n)=n f ( n ) = n 与 μ(n) μ ( n ) 均为积性函数
所以 g(n)=∑d|nd⋅μ(nd) g ( n ) = ∑ d | n d ⋅ μ ( n d ) 为积性函数
若: n=pe11⋅pe22⋯pekkp1,p2,⋯,pk n = p 1 e 1 ⋅ p 2 e 2 ⋯ p k e k p 1 , p 2 , ⋯ , p k 均为质数
那么: g(n)=g(pe11)⋅g(pe22)×⋯×g(pekk) g ( n ) = g ( p 1 e 1 ) ⋅ g ( p 2 e 2 ) × ⋯ × g ( p k e k )
考虑: g(pk)=∑d|pkd⋅μ(pkd)p g ( p k ) = ∑ d | p k d ⋅ μ ( p k d ) p 为质数
g(pk)=∑i=0kpk−i⋅μ(pi)=pk−pk−1=φ(pk) g ( p k ) = ∑ i = 0 k p k − i ⋅ μ ( p i ) = p k − p k − 1 = φ ( p k )
所以: g(n)=φ(n) g ( n ) = φ ( n )
∑k=1n⌊nk⌋⋅⌊mk⌋∑d|kd⋅μ(kd)=∑k=1n⌊nk⌋⋅⌊mk⌋⋅φ(k) ∑ k = 1 n ⌊ n k ⌋ ⋅ ⌊ m k ⌋ ∑ d | k d ⋅ μ ( k d ) = ∑ k = 1 n ⌊ n k ⌋ ⋅ ⌊ m k ⌋ ⋅ φ ( k )
复杂度: O(n+q⋅(n−−√+m−−√)) O ( n + q ⋅ ( n + m ) )
7. 1≤n=m≤1011 1 ≤ n = m ≤ 10 11
用杜教筛来求欧拉函数 φ(x) φ ( x ) 前缀和 s(n) s ( n )
s(n)=(n+1)⋅n2−∑i=2ns(⌊ni⌋) s ( n ) = ( n + 1 ) ⋅ n 2 − ∑ i = 2 n s ( ⌊ n i ⌋ )
∑d=1nd⋅(2⋅s(⌊nd⌋)−1) ∑ d = 1 n d ⋅ ( 2 ⋅ s ( ⌊ n d ⌋ ) − 1 ) (3)
或
∑k=1n⌊nk⌋2⋅φ(k) ∑ k = 1 n ⌊ n k ⌋ 2 ⋅ φ ( k ) (6)
复杂度: O(n23) O ( n 2 3 )
对于 m=n m = n 的情况
我们得到了 2 2 个不同的等式:
∑i=1n∑j=1ngcd(i,j)=⎧⎩⎨⎪⎪⎪⎪⎪⎪∑d=1nd⋅(2⋅s(⌊nd⌋)−1)(s(n)=∑i=1nφ(i)) ∑k=1n⌊nk⌋2⋅φ(k) ∑ i = 1 n ∑ j = 1 n g c d ( i , j ) = { ∑ d = 1 n d ⋅ ( 2 ⋅ s ( ⌊ n d ⌋ ) − 1 ) ( s ( n ) = ∑ i = 1 n φ ( i ) ) ∑ k = 1 n ⌊ n k ⌋ 2 ⋅ φ ( k )
它们之间是不是有一些联系呢?
∑d=1nd⋅(2⋅s(⌊nd⌋)−1)=(∑d=1n2⋅d⋅s(⌊nd⌋))−(n+1)⋅n2=(∑d=1n2⋅d⋅s(⌊nd⌋))−∑d=1ns(⌊nd⌋)=∑d=1n(2⋅d−1)⋅s(⌊nd⌋)=∑d=1n(2⋅d−1)∑i=1⌊nd⌋φ(i)=∑d=1n∑i=1⌊nd⌋(2⋅d−1)⋅φ(i)=∑d=1n∑d|i(2⋅d−1)⋅φ(id)=∑i=1n∑d|i(2⋅d−1)⋅φ(id)=∑i=1n(f×g)(i)(11)(12)(13)(14)(15)(16)(17)(18)(19) (11) ∑ d = 1 n d ⋅ ( 2 ⋅ s ( ⌊ n d ⌋ ) − 1 ) (12) = ( ∑ d = 1 n 2 ⋅ d ⋅ s ( ⌊ n d ⌋ ) ) − ( n + 1 ) ⋅ n 2 (13) = ( ∑ d = 1 n 2 ⋅ d ⋅ s ( ⌊ n d ⌋ ) ) − ∑ d = 1 n s ( ⌊ n d ⌋ ) (14) = ∑ d = 1 n ( 2 ⋅ d − 1 ) ⋅ s ( ⌊ n d ⌋ ) (15) = ∑ d = 1 n ( 2 ⋅ d − 1 ) ∑ i = 1 ⌊ n d ⌋ φ ( i ) (16) = ∑ d = 1 n ∑ i = 1 ⌊ n d ⌋ ( 2 ⋅ d − 1 ) ⋅ φ ( i ) (17) = ∑ d = 1 n ∑ d | i ( 2 ⋅ d − 1 ) ⋅ φ ( i d ) (18) = ∑ i = 1 n ∑ d | i ( 2 ⋅ d − 1 ) ⋅ φ ( i d ) (19) = ∑ i = 1 n ( f × g ) ( i )
其中 {f(x)=φ(x)g(x)=2⋅x−1 { f ( x ) = φ ( x ) g ( x ) = 2 ⋅ x − 1
∑k=1n⌊nk⌋2⋅φ(k)=∑k=1n∑i=1⌊nk⌋(2⋅i−1)⋅φ(k)=∑k=1n∑k|i(2⋅ik−1)⋅φ(k)=∑i=1n∑k|i(2⋅ik−1)⋅φ(k)=∑i=1n(g×f)(i)(20)(21)(22)(23)(24) (20) ∑ k = 1 n ⌊ n k ⌋ 2 ⋅ φ ( k ) (21) = ∑ k = 1 n ∑ i = 1 ⌊ n k ⌋ ( 2 ⋅ i − 1 ) ⋅ φ ( k ) (22) = ∑ k = 1 n ∑ k | i ( 2 ⋅ i k − 1 ) ⋅ φ ( k ) (23) = ∑ i = 1 n ∑ k | i ( 2 ⋅ i k − 1 ) ⋅ φ ( k ) (24) = ∑ i = 1 n ( g × f ) ( i )
所以这两个等式都表达的是同一个狄利克雷卷积的前缀和, 自然也就相等了