gcd求和的几种方法

最大公约数 : 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. 1n106 1 ≤ n ≤ 10 6

相信大家都会

for(int i=1;i<=n;++i)
    ans+=gcd(n,i);

2. 1n109 1 ≤ n ≤ 10 9

设: gcd(n,i)=d g c d ( n , i ) = d
显然: n%d=0 n % d = 0
原问题转换为:
d|ndi=1n[gcd(n,i)=d] ∑ d | n d ⋅ ∑ i = 1 n [ g c d ( n , i ) = d ]
d|ndi=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. 1n1012 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=pe11pe22pekkp1,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=0kpkiφ(pi)=i=1kpkiφ(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=1kpki(pipi1)=pk+i=1k(pkpk1) 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)pkkpk1 g ( p k ) = ( k + 1 ) ⋅ p k − k ⋅ p k − 1

复杂度: O(n) O ( n )


二维形式: i=1nj=1mgcd(i,j) ∑ i = 1 n ∑ j = 1 m g c d ( i , j )

1. 1n,m103 1 ≤ n , m ≤ 10 3

相信大家都会

for(int i=1;i<=n;++i)
    for(int j=1;j<=m;++j)
        ans+=gcd(i,j);

2. 1n=m105q 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=1nj=1ngcd(i,j)=2i=1nj=1igcd(i,j)i=1ngcd(i,i)=2i=1nj=1igcd(i,j)(n+1)n2=2i=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. 1n=m107q 1 ≤ n = m ≤ 10 7 q 组询问

解法1:

枚举: gcd(i,j)=d[1,n] g c d ( i , j ) = d ∈ [ 1 , n ]
原问题转换为:

d=1ndi=1nj=1n[gcd(i,j)=d] ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = d ]

d=1ndi=1ndj=1nd[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=1nj=1n[gcd(i,j)=1] f ( n ) = ∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = 1 ]
设: g(n)=i=1nj=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)=2g(n)1 f ( n ) = 2 ⋅ g ( n ) − 1

d=1ndi=1ndj=1nd[gcd(i,j)=1]=d=1ndf(nd)=d=1nd(2g(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(2g(nd)1) ∑ d = 1 n d ⋅ ( 2 ⋅ g ( ⌊ n d ⌋ ) − 1 ) 可以分块

复杂度: O(n+qn) O ( n + q ⋅ n )

解法2:

i=1nj=1ngcd(i,j)=2i=1nj=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=1nj=1igcd(i,j)=i=1nd|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. 1nm105 1 ≤ n ≤ m ≤ 10 5

枚举: gcd(i,j)=d[1,n] g c d ( i , j ) = d ∈ [ 1 , n ]

原问题转换为:
d=1ndi=1nj=1m[gcd(i,j)=d] ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = d ]

设: g(x)=i=1nj=1m[gcd(i,j)=x] g ( x ) = ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = x ]

设: f(x)=i=1nj=1m[xgcd(i,j)]=nxmx 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)ndmd g ( x ) = ∑ x | d μ ( d x ) ⋅ ⌊ n d ⌋ ⋅ ⌊ m d ⌋

d=1ndd|iμ(id)nimi ∑ d = 1 n d ∑ d | i μ ( i d ) ⋅ ⌊ n i ⌋ ⋅ ⌊ m i ⌋

复杂度: O(nlogn) O ( n l o g n )


5. 1nm106 1 ≤ n ≤ m ≤ 10 6

d=1ndi=1nj=1m[gcd(i,j)=d]d=1ndi=1ndj=1md[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=1ndj=1md[gcd(i,j)=1]=i=1ndμ(i)ndimdi ∑ 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=1ndi=1ndμ(i)ndimdi ∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ μ ( i ) ⋅ ⌊ ⌊ n d ⌋ i ⌋ ⋅ ⌊ ⌊ m d ⌋ i ⌋

令: f(x,y)=i=1xμ(i)xiyi f ( x , y ) = ∑ i = 1 x μ ( i ) ⋅ ⌊ x i ⌋ ⋅ ⌊ y i ⌋

利用数论分块可以在 O(x+y) O ( x + y ) 得到函数值

同样的, 已知 f(x,y) f ( x , y ) 的值, d=1ndf(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. 1nm107q 1 ≤ n ≤ m ≤ 10 7 q 组询问

d=1ndi=1ndμ(i)ndimdi=d=1ndi=1ndμ(i)nidmid ∑ 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=idk[1,n] k = i ⋅ d k ∈ [ 1 , n ]

枚举 k k : k=1nd|kdμ(kd)nkmk=k=1nnkmkd|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=pe11pe22pekkp1,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=0kpkiμ(pi)=pkpk1=φ(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=1nnkmkd|kdμ(kd)=k=1nnkmkφ(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. 1n=m1011 1 ≤ n = m ≤ 10 11

用杜教筛来求欧拉函数 φ(x) φ ( x ) 前缀和 s(n) s ( n )

s(n)=(n+1)n2i=2ns(ni) s ( n ) = ( n + 1 ) ⋅ n 2 − ∑ i = 2 n s ( ⌊ n i ⌋ )

d=1nd(2s(nd)1) ∑ d = 1 n d ⋅ ( 2 ⋅ s ( ⌊ n d ⌋ ) − 1 ) (3)

k=1nnk2φ(k) ∑ k = 1 n ⌊ n k ⌋ 2 ⋅ φ ( k ) (6)

复杂度: O(n23) O ( n 2 3 )


总结:

对于 m=n m = n 的情况
我们得到了 2 2 个不同的等式:
i=1nj=1ngcd(i,j)=d=1nd(2s(nd)1)(s(n)=i=1nφ(i)) k=1nnk2φ(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(2s(nd)1)=(d=1n2ds(nd))(n+1)n2=(d=1n2ds(nd))d=1ns(nd)=d=1n(2d1)s(nd)=d=1n(2d1)i=1ndφ(i)=d=1ni=1nd(2d1)φ(i)=d=1nd|i(2d1)φ(id)=i=1nd|i(2d1)φ(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)=2x1 { f ( x ) = φ ( x ) g ( x ) = 2 ⋅ x − 1

k=1nnk2φ(k)=k=1ni=1nk(2i1)φ(k)=k=1nk|i(2ik1)φ(k)=i=1nk|i(2ik1)φ(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 )
所以这两个等式都表达的是同一个狄利克雷卷积的前缀和, 自然也就相等了

你可能感兴趣的:(数论)