首页 > 代码库 > (转载)有关反演和gcd
(转载)有关反演和gcd
tips :
积性函数 F (n) = Π F (piai )
若F (n), G (n)是积性函数则
F (n) * G (n)
Σd | n F (n)
是积性函数
n = Σd | n φ (d)
1 = Σd | n μ (d)
Σgcd (i, n) = 1 i = n * φ (n) / 2
Problem1
F (n) = Σ1<= i <= n gcd(i, n), n <= 1000000
Sol
枚举结果
F (n) = Σd | n d * Σgcd (i, n) = d 1
F (n) = Σd | n d * Σgcd (i / d, n / d) = 1 1
F (n) = Σd | n d * Σgcd (i / d, n / d) = 1 1
F (n) = Σd | n d * φ (n / d)
单次计算O (sqrt N)
筛法O (N)
Problem 2
F (n) = Σ1<= i <= n gcd (i, n), n <= 2147483647 (POJ longge‘s problem)
Sol
由P1可知F (n)是积性函数 因此考虑计算F (pk)
由P1 易知
F (pk) = p * F (pk - 1) + (p - 1)pk - 1
单个F(n)可以O (sqrt N)时间有他的质因子分解计算得到
Problem 3
F (n) = Σ1<= i <= n lcm(i, n), n <= 1000000 (SPOJ LCMSUM)
显而易见的变形
F (n) = Σ1<= i <= n i * n / gcd (i, n)
F (n) =Σd | n Σgcd (i, n) = d i * n / d
F (n) =Σd | n n / d * Σgcd (i, n) = d i
F (n) =Σd | n n / d * Σgcd (i / d, n / d) = 1 i
F (n) =Σd | n n / d * d * Σgcd (i / d, n / d) = 1 i / d
令j = i / d
F (n) =Σd | n n * Σgcd (j, n / d) = 1 j
由Σgcd (i, n) = 1 i = n * φ (n) / 2
F (n) =Σd | n n * (n / d) * φ (n / d) / 2
F (n) =n * Σd | n (n / d) * φ (n / d) / 2
F (n) =n / 2 * Σd | n d * φ (d)
筛出 Σd | n d * φ (d) O (N)-O(1)
Problem 4
F (n) = Σ1<= i <= n Σ1<= j <= n gcd (i, j) n <= 1000000 (SPOJ GCDEX)
Sol
G (n) = Σd | n d * φ (n / d)
F (n) = Σ1<= i <= n G (i)
筛出G (i) 前缀和
Problem 5
求F (n, m) = [n / d] * [m / d]
Sol
研究退化情况 m = 1 F (n) = [n / d]
共有sqrt n种不同取值
F (n, m) = [n / d] * [m / d]
共有sqrt n + sqrt m种不同取值 归并这两种取值
Problem 6
多组询问n, m 求F (n, m) = Σ1<= i <= n Σ1<= j <= m gcd (i, j)
Sol
F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ d | gcd (i, j) φ (d)
F (n, m) = Σ d φ (d) Σ1<= i <= n d | i Σ1<= j <= m d | j 1
F (n, m) = Σ d φ (d) * [n / d] * [m / d]
可以经P5解决
Problem 6
多组询问n, m 求F (n, m) = Σ1<= i <= n Σ1<= j <= m gcd (i, j) = 1 1
Sol
F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ d | gcd (i, j) μ (d)
F (n, m) = Σ d μ (d) Σ1<= i <= n d | i Σ1<= j <= m d | j 1
F (n, m) = Σ d μ (d) * [n / d] * [m / d]
可以经P5解决
Problem 7
F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ gcd (i, j) <- prime 1 (BZOJ YY的GCD)
Sol
F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ gcd (i, j) <- prime 1
F (n, m) = Σ1<= i <= n Σ1<= j <= m Σ p <- prime gcd (i, j) = p 1
F (n, m) = Σ p <- prime Σ1<= i <= n Σ1<= j <= m gcd (i, j) = p 1
F (n, m) = Σ p <- prime Σ1<= i <= n / p Σ1<= j <= m / p gcd (n / p, m / p) = 1 1
可以经P6解决