如题。本文主要整理莫反习题的做法以及一些推式子技巧。
P2522 [HAOI2011] Problem b
题目让我们求
∑ i = a b ∑ j = c d [ gcd ( i , j ) = k ] \sum_{i=a}^{b}\sum_{j=c}^{d}[\gcd(i,j)=k]
i = a ∑ b j = c ∑ d [ g cd( i , j ) = k ]
我们只需要会求
∑ i = 1 x ∑ j = 1 y [ gcd ( i , j ) = k ] \sum_{i=1}^{x}\sum_{j=1}^{y}[\gcd(i,j)=k]
i = 1 ∑ x j = 1 ∑ y [ g cd( i , j ) = k ]
然后容斥一下就好了。
∑ i = 1 x ∑ j = 1 y [ gcd ( i , j ) = k ] = ∑ i = 1 x / k ∑ j = 1 y / k [ gcd ( i , j ) = 1 ] = ∑ i = 1 x / k ∑ j = 1 y / k ∑ d ∣ i , d ∣ j μ ( d ) = ∑ i = 1 x / k ∑ j = 1 y / k ∑ d μ ( d ) [ d ∣ i ] [ d ∣ j ] ⋯ ⋯ ① = ∑ d μ ( d ) ∑ i = 1 x / k [ d ∣ i ] ∑ j = 1 y / k [ d ∣ j ] = ∑ d = 1 min { ⌊ x / k ⌋ , ⌊ y / k ⌋ } μ ( d ) ⌊ x k d ⌋ ⌊ y k d ⌋ \begin{aligned}
\sum_{i=1}^{x}\sum_{j=1}^{y}[\gcd(i,j)=k]
& = \sum_{i=1}^{x/k}\sum_{j=1}^{y/k}[\gcd(i,j)=1] \\
& = \sum_{i=1}^{x/k}\sum_{j=1}^{y/k}\sum_{d|i,d|j}\mu(d) \\
& = \sum_{i=1}^{x/k}\sum_{j=1}^{y/k}\sum_{d}\mu(d)[d|i][d|j] & \cdots\cdots\text{①} \\
& = \sum_{d}\mu(d)\sum_{i=1}^{x/k}[d|i]\sum_{j=1}^{y/k}[d|j] \\
& = \sum_{d=1}^{\min\{\lfloor x/k\rfloor ,\lfloor y/k\rfloor \}}\mu(d)\left\lfloor\frac{x}{kd}\right\rfloor\left\lfloor\frac{y}{kd}\right\rfloor
\end{aligned}
i = 1 ∑ x j = 1 ∑ y [ g cd( i , j ) = k ] = i = 1 ∑ x / k j = 1 ∑ y / k [ g cd( i , j ) = 1 ] = i = 1 ∑ x / k j = 1 ∑ y / k d ∣ i , d ∣ j ∑ μ ( d ) = i = 1 ∑ x / k j = 1 ∑ y / k d ∑ μ ( d ) [ d ∣ i ] [ d ∣ j ] = d ∑ μ ( d ) i = 1 ∑ x / k [ d ∣ i ] j = 1 ∑ y / k [ d ∣ j ] = d = 1 ∑ m i n { ⌊ x / k ⌋ , ⌊ y / k ⌋ } μ ( d ) ⌊ k d x ⌋ ⌊ k d y ⌋ ⋯ ⋯ ①
技巧:在不会跳步的时候,建议使用 ① 处方法,使用艾弗森括号代替 ∑ \sum ∑ 下的条件进行求和顺序的交换。
P3455 [POI2007] ZAP-Queries
同 P2522。
P2257 YY的GCD
∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) is prime ] = ∑ p is prime ∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = p ] = ∑ p is prime ∑ i = 1 n / p ∑ j = 1 m / p [ gcd ( i , j ) = 1 ] = ∑ p is prime ∑ i = 1 n / p ∑ j = 1 m / p ∑ d ∣ i , d ∣ j μ ( d ) = ∑ p is prime ∑ d μ ( d ) ⌊ n d p ⌋ ⌊ m d p ⌋ = ∑ q ⌊ n q ⌋ ⌊ m q ⌋ ∑ p ∣ q , p is prime μ ( q p ) ⋯ ⋯ ① \begin{aligned}
\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)\text{ is prime}]
& = \sum_{p\text{ is prime}}\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=p] \\
& = \sum_{p\text{ is prime}}\sum_{i=1}^{n/p}\sum_{j=1}^{m/p}[\gcd(i,j)=1] \\
& = \sum_{p\text{ is prime}}\sum_{i=1}^{n/p}\sum_{j=1}^{m/p}\sum_{d|i,d|j}\mu(d) \\
& = \sum_{p\text{ is prime}}\sum_{d}\mu(d)\left\lfloor\frac{n}{dp}\right\rfloor\left\lfloor\frac{m}{dp}\right\rfloor \\
& = \sum_{q}\left\lfloor\frac{n}{q}\right\rfloor\left\lfloor\frac{m}{q}\right\rfloor\sum_{p|q,p\text{ is prime}}\mu(\frac{q}{p}) & \cdots\cdots\text{①}
\end{aligned}
i = 1 ∑ n j = 1 ∑ m [ g cd( i , j ) is prime ] = p is prime ∑ i = 1 ∑ n j = 1 ∑ m [ g cd( i , j ) = p ] = p is prime ∑ i = 1 ∑ n / p j = 1 ∑ m / p [ g cd( i , j ) = 1 ] = p is prime ∑ i = 1 ∑ n / p j = 1 ∑ m / p d ∣ i , d ∣ j ∑ μ ( d ) = p is prime ∑ d ∑ μ ( d ) ⌊ d p n ⌋ ⌊ d p m ⌋ = q ∑ ⌊ q n ⌋ ⌊ q m ⌋ p ∣ q , p is prime ∑ μ ( p q ) ⋯ ⋯ ①
观察到
f ( x ) = ∑ p ∣ x , p is prime μ ( x p ) f(x) = \sum_{p|x,p\text{ is prime}}\mu(\frac{x}{p}) \\
f ( x ) = p ∣ x , p is prime ∑ μ ( p x )
是积性函数。对其进行线性筛然后整除分块处理原式即可。
技巧: ① 处,考虑到下取整除法的分母是 d p dp d p ,于是令 q = d p q=dp q = d p 转而枚举 q q q ,便于进行整除分块。
SP5971 LCMSUM - LCM Sum
∑ i = 1 n lcm ( i , n ) = n ∑ i = 1 n i gcd ( i , n ) = n ∑ d ∣ n 1 d ∑ i = 1 n i [ gcd ( i , n ) = d ] = n ∑ d ∣ n 1 d ∑ i = 1 n / d i d [ gcd ( i , n d ) = 1 ] = n ∑ d ∣ n ∑ i = 1 n / d i ∑ k ∣ i , k ∣ n d μ ( k ) = n ∑ d ∣ n ∑ i = 1 n / d i ∑ k [ k ∣ i ] [ d k ∣ n ] μ ( k ) = n ∑ d ∣ n ∑ k ∣ n d k μ ( k ) ∑ i = 1 n / k d i 令 f ( x ) = x ( x + 1 ) 2 , 则 原式 = n ∑ d ∣ n ∑ k ∣ n d k μ ( k ) f ( n k d ) = n ∑ q ∣ n f ( n q ) ∑ k ∣ q k μ ( k ) 令 g ( x ) = ∑ k ∣ x k μ ( k ) , 则 原式 = n ∑ q ∣ n g ( q ) f ( n q ) \begin{aligned}
\sum_{i=1}^{n}\operatorname{lcm}(i,n)
& = n\sum_{i=1}^{n}\frac{i}{\gcd(i,n)} \\
& = n\sum_{d|n}\frac{1}{d}\sum_{i=1}^{n}i[\gcd(i,n)=d] \\
& = n\sum_{d|n}\frac{1}{d}\sum_{i=1}^{n/d}id[\gcd(i,\frac{n}{d})=1] \\
& = n\sum_{d|n}\sum_{i=1}^{n/d}i\sum_{k|i,k|\frac{n}{d}}\mu(k) \\
& = n\sum_{d|n}\sum_{i=1}^{n/d}i\sum_k[k|i][dk|n]\mu(k) \\
& = n\sum_{d|n}\sum_{k|\frac{n}{d}}k\mu(k)\sum_{i=1}^{n/kd}i \\
\text{令 }f(x) & = \frac{x(x+1)}{2},\text{ 则} \\
\text{原式}
& = n\sum_{d|n}\sum_{k|\frac{n}{d}}k\mu(k)f(\frac{n}{kd}) \\
& = n\sum_{q|n}f(\frac{n}{q})\sum_{k|q}k\mu(k) \\
\text{令 }g(x) & = \sum_{k|x}k\mu(k),\text{ 则} \\
\text{原式}
& = n\sum_{q|n}g(q)f(\frac{n}{q})
\end{aligned}
i = 1 ∑ n l c m ( i , n ) 令 f ( x ) 原式 令 g ( x ) 原式 = n i = 1 ∑ n g cd( i , n ) i = n d ∣ n ∑ d 1 i = 1 ∑ n i [ g cd( i , n ) = d ] = n d ∣ n ∑ d 1 i = 1 ∑ n / d i d [ g cd( i , d n ) = 1 ] = n d ∣ n ∑ i = 1 ∑ n / d i k ∣ i , k ∣ d n ∑ μ ( k ) = n d ∣ n ∑ i = 1 ∑ n / d i k ∑ [ k ∣ i ] [ d k ∣ n ] μ ( k ) = n d ∣ n ∑ k ∣ d n ∑ k μ ( k ) i = 1 ∑ n / k d i = 2 x ( x + 1 ) , 则 = n d ∣ n ∑ k ∣ d n ∑ k μ ( k ) f ( k d n ) = n q ∣ n ∑ f ( q n ) k ∣ q ∑ k μ ( k ) = k ∣ x ∑ k μ ( k ) , 则 = n q ∣ n ∑ g ( q ) f ( q n )
观察到 g g g 是积性函数,线性筛出它。
答案是卷积形式,用类似埃氏筛的方法预处理出它。
P1829 [国家集训队] Crash 的数字表格 / JZPTAB
不妨设 n ≤ m n\le m n ≤ m 。
令 S ( x ) = x ( x + 1 ) 2 S(x)=\frac{x(x+1)}{2} S ( x ) = 2 x ( x + 1 ) 。
∑ i = 1 n ∑ j = 1 m lcm ( i , j ) = ∑ i = 1 n ∑ j = 1 m i j gcd ( i , j ) = ∑ d = 1 n 1 d ∑ i = 1 n ∑ j = 1 m i j [ gcd ( i , j ) = d ] = ∑ d = 1 n d ∑ i = 1 n / d ∑ j = 1 m / d i j [ gcd ( i , j ) = 1 ] = ∑ d = 1 n d ∑ i = 1 n / d ∑ j = 1 m / d i j ∑ k ∣ i , k ∣ j μ ( k ) = ∑ d = 1 n d ∑ k = 1 n / d k 2 μ ( k ) ∑ i = 1 n d k i ∑ j = 1 m d k j = ∑ d = 1 n d ∑ k = 1 n / d k 2 μ ( k ) S ( ⌊ n d k ⌋ ) S ( ⌊ m d k ⌋ ) = ∑ g = 1 n g S ( ⌊ n g ⌋ ) S ( ⌊ m g ⌋ ) ∑ k ∣ g k μ ( k ) \begin{aligned}
\sum_{i=1}^{n}\sum_{j=1}^{m}\operatorname{lcm}(i,j)
& = \sum_{i=1}^{n}\sum_{j=1}^{m}\frac{ij}{\gcd(i,j)} \\
& = \sum_{d=1}^{n}\frac{1}{d}\sum_{i=1}^{n}\sum_{j=1}^{m}ij[\gcd(i,j)=d] \\
& = \sum_{d=1}^{n}d\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}ij[\gcd(i,j)=1] \\
& = \sum_{d=1}^{n}d\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}ij\sum_{k|i,k|j}\mu(k) \\
& = \sum_{d=1}^{n}d\sum_{k=1}^{n/d}k^2\mu(k)\sum_{i=1}^{\frac{n}{dk}}i\sum_{j=1}^{\frac{m}{dk}}j \\
& = \sum_{d=1}^{n}d\sum_{k=1}^{n/d}k^2\mu(k)S(\left\lfloor\frac{n}{dk}\right\rfloor)S(\left\lfloor\frac{m}{dk}\right\rfloor) \\
& = \sum_{g=1}^{n}gS(\left\lfloor\frac{n}{g}\right\rfloor)S(\left\lfloor\frac{m}{g}\right\rfloor)\sum_{k|g}k\mu(k)
\end{aligned}
i = 1 ∑ n j = 1 ∑ m l c m ( i , j ) = i = 1 ∑ n j = 1 ∑ m g cd( i , j ) i j = d = 1 ∑ n d 1 i = 1 ∑ n j = 1 ∑ m i j [ g cd( i , j ) = d ] = d = 1 ∑ n d i = 1 ∑ n / d j = 1 ∑ m / d i j [ g cd( i , j ) = 1 ] = d = 1 ∑ n d i = 1 ∑ n / d j = 1 ∑ m / d i j k ∣ i , k ∣ j ∑ μ ( k ) = d = 1 ∑ n d k = 1 ∑ n / d k 2 μ ( k ) i = 1 ∑ d k n i j = 1 ∑ d k m j = d = 1 ∑ n d k = 1 ∑ n / d k 2 μ ( k ) S ( ⌊ d k n ⌋ ) S ( ⌊ d k m ⌋ ) = g = 1 ∑ n g S ( ⌊ g n ⌋ ) S ( ⌊ g m ⌋ ) k ∣ g ∑ k μ ( k )
因为
f ( x ) = ∑ k ∣ x k μ ( k ) f(x)=\sum_{k|x}k\mu(k)
f ( x ) = k ∣ x ∑ k μ ( k )
是积性函数,我们线性筛出它即可。
P3327 [SDOI2015]约数个数和
不妨设 n ≤ m n\le m n ≤ m 。
∑ 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 ∑ y [ x ∣ i ] [ y ∣ j ] [ gcd ( x , y ) = 1 ] = ∑ x = 1 n ∑ y = 1 m [ g c d ( x , y ) = 1 ] ⌊ n x ⌋ ⌊ m y ⌋ = ∑ x = 1 n ∑ y = 1 m ∑ d ∣ x , d ∣ y μ ( d ) ⌊ n x ⌋ ⌊ m y ⌋ = ∑ d μ ( d ) ∑ x = 1 n / d ∑ y = 1 m / d ⌊ n d x ⌋ ⌊ m d y ⌋ = ∑ d = 1 n μ ( d ) ∑ x = 1 n / d ⌊ n d x ⌋ ∑ y = 1 m / d ⌊ m d y ⌋ \begin{aligned}
\sum_{i=1}^{n}\sum_{j=1}^{m}d(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\sum_y[x|i][y|j][\gcd(x,y)=1] \\
& = \sum_{x=1}^{n}\sum_{y=1}^{m}[gcd(x,y)=1]\left\lfloor\frac{n}{x}\right\rfloor\left\lfloor\frac{m}{y}\right\rfloor \\
& = \sum_{x=1}^{n}\sum_{y=1}^{m}\sum_{d|x,d|y}\mu(d)\left\lfloor\frac{n}{x}\right\rfloor\left\lfloor\frac{m}{y}\right\rfloor \\
& = \sum_{d}\mu(d)\sum_{x=1}^{n/d}\sum_{y=1}^{m/d}\left\lfloor\frac{n}{dx}\right\rfloor\left\lfloor\frac{m}{dy}\right\rfloor \\
& = \sum_{d=1}^{n}\mu(d)\sum_{x=1}^{n/d}\left\lfloor\frac{n}{dx}\right\rfloor\sum_{y=1}^{m/d}\left\lfloor\frac{m}{dy}\right\rfloor
\end{aligned}
i = 1 ∑ n j = 1 ∑ m d ( i j ) = i = 1 ∑ n j = 1 ∑ m x ∣ i ∑ y ∣ j ∑ [ g cd( x , y ) = 1 ] = i = 1 ∑ n j = 1 ∑ m x ∑ y ∑ [ x ∣ i ] [ y ∣ j ] [ g cd( x , y ) = 1 ] = x = 1 ∑ n y = 1 ∑ m [ g c d ( x , y ) = 1 ] ⌊ x n ⌋ ⌊ y m ⌋ = x = 1 ∑ n y = 1 ∑ m d ∣ x , d ∣ y ∑ μ ( d ) ⌊ x n ⌋ ⌊ y m ⌋ = d ∑ μ ( d ) x = 1 ∑ n / d y = 1 ∑ m / d ⌊ d x n ⌋ ⌊ d y m ⌋ = d = 1 ∑ n μ ( d ) x = 1 ∑ n / d ⌊ d x n ⌋ y = 1 ∑ m / d ⌊ d y m ⌋
使用整除分块预处理
f ( x ) = ∑ i = 1 x ⌊ x i ⌋ f(x)=\sum_{i=1}^{x}\left\lfloor\frac{x}{i}\right\rfloor
f ( x ) = i = 1 ∑ x ⌊ i x ⌋
因此,
∑ i = 1 n ∑ j = 1 m d ( i j ) = ∑ d = 1 n μ ( d ) ∑ x = 1 n / d ⌊ n d x ⌋ ∑ y = 1 m / d ⌊ m d y ⌋ = ∑ d = 1 n μ ( d ) f ( ⌊ n d ⌋ ) f ( ⌊ m d ⌋ ) \begin{aligned}
\sum_{i=1}^{n}\sum_{j=1}^{m}d(ij)
& = \sum_{d=1}^{n}\mu(d)\sum_{x=1}^{n/d}\left\lfloor\frac{n}{dx}\right\rfloor\sum_{y=1}^{m/d}\left\lfloor\frac{m}{dy}\right\rfloor \\
& = \sum_{d=1}^{n}\mu(d)f(\left\lfloor\frac{n}{d}\right\rfloor)f(\left\lfloor\frac{m}{d}\right\rfloor)
\end{aligned}
i = 1 ∑ n j = 1 ∑ m d ( i j ) = d = 1 ∑ n μ ( d ) x = 1 ∑ n / d ⌊ d x n ⌋ y = 1 ∑ m / d ⌊ d y m ⌋ = d = 1 ∑ n μ ( d ) f ( ⌊ d n ⌋ ) f ( ⌊ d m ⌋ )
整除分块处理每个询问即可。
P3312 [SDOI2014]数表
先不考虑 ≤ a \le a ≤ a 的限制条件。
设 f ( d ) f(d) f ( d ) 表示 d d d 的约数和。
不妨设 n ≤ m n\le m n ≤ m 。
∑ i = 1 n ∑ j = 1 m ∑ d ∣ i , d ∣ j d = ∑ d = 1 n f ( d ) ∑ i = 1 n / d ∑ j = 1 m / d [ gcd ( i , j ) = 1 ] = ∑ d = 1 n f ( d ) ∑ i = 1 n / d ∑ j = 1 m / d ∑ k ∣ i , k ∣ j μ ( k ) = ∑ d = 1 n f ( d ) ∑ k = 1 n μ ( k ) ⌊ n d k ⌋ ⌊ m d k ⌋ = ∑ q = 1 n ⌊ n q ⌋ ⌊ m q ⌋ ∑ d ∣ q f ( d ) μ ( q d ) \begin{aligned}
\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{d|i,d|j}d
& = \sum_{d=1}^{n}f(d)\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}[\gcd(i,j)=1] \\
& = \sum_{d=1}^{n}f(d)\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}\sum_{k|i,k|j}\mu(k) \\
& = \sum_{d=1}^{n}f(d)\sum_{k=1}^{n}\mu(k)\left\lfloor\frac{n}{dk}\right\rfloor\left\lfloor\frac{m}{dk}\right\rfloor \\
& = \sum_{q=1}^{n}\left\lfloor\frac{n}{q}\right\rfloor\left\lfloor\frac{m}{q}\right\rfloor\sum_{d|q}f(d)\mu\left(\frac{q}{d}\right)
\end{aligned}
i = 1 ∑ n j = 1 ∑ m d ∣ i , d ∣ j ∑ d = d = 1 ∑ n f ( d ) i = 1 ∑ n / d j = 1 ∑ m / d [ g cd( i , j ) = 1 ] = d = 1 ∑ n f ( d ) i = 1 ∑ n / d j = 1 ∑ m / d k ∣ i , k ∣ j ∑ μ ( k ) = d = 1 ∑ n f ( d ) k = 1 ∑ n μ ( k ) ⌊ d k n ⌋ ⌊ d k m ⌋ = q = 1 ∑ n ⌊ q n ⌋ ⌊ q m ⌋ d ∣ q ∑ f ( d ) μ ( d q )
令
g ( x ) = ∑ d ∣ x f ( d ) μ ( x d ) g(x)=\sum_{d|x}f(d)\mu\left(\frac{x}{d}\right)
g ( x ) = d ∣ x ∑ f ( d ) μ ( d x )
对于每个询问,只有 ≤ a \le a ≤ a 的 f ( d ) f(d) f ( d ) 才会被统计到。
那么,我们预处理出所有的 f ( x ) f(x) f ( x ) 并进行排序,然后将询问离线按 a a a 排序。
每次 a a a 增加的时候,对影响到的 g ( x ) g(x) g ( x ) 累加贡献。
我们需要快速单点修改 g ( x ) g(x) g ( x ) ,并快速得到 g ( x ) g(x) g ( x ) 的前缀和。
使用树状数组维护 g ( x ) g(x) g ( x ) 即可。
P4449 于神之怒加强版
不妨设 n ≤ m n\le m n ≤ m 。
∑ i = 1 n ∑ j = 1 m gcd ( i , j ) k = ∑ d d k ∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = d ] = ∑ d d k ∑ i = 1 n / d ∑ j = 1 m / d [ gcd ( i , j ) = 1 ] = ∑ d d k ∑ i = 1 n / d ∑ j = 1 m / d ∑ g ∣ i , g ∣ j μ ( g ) = ∑ d = 1 n d k ∑ g = 1 n / d μ ( g ) ⌊ n d g ⌋ ⌊ m d g ⌋ = ∑ h = 1 n ⌊ n h ⌋ ⌊ m h ⌋ ∑ d ∣ h d k μ ( h d ) \begin{aligned}
\sum_{i=1}^{n}\sum_{j=1}^{m}\gcd(i,j)^k
& = \sum_{d}d^k\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=d] \\
& = \sum_{d}d^k\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}[\gcd(i,j)=1] \\
& = \sum_{d}d^k\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}\sum_{g|i,g|j}\mu(g) \\
& = \sum_{d=1}^{n}d^k\sum_{g=1}^{n/d}\mu(g)\left\lfloor\frac{n}{dg}\right\rfloor\left\lfloor\frac{m}{dg}\right\rfloor \\
& = \sum_{h=1}^{n}\left\lfloor\frac{n}{h}\right\rfloor\left\lfloor\frac{m}{h}\right\rfloor\sum_{d|h}d^k\mu\left(\frac{h}{d}\right)
\end{aligned}
i = 1 ∑ n j = 1 ∑ m g cd( i , j ) k = d ∑ d k i = 1 ∑ n j = 1 ∑ m [ g cd( i , j ) = d ] = d ∑ d k i = 1 ∑ n / d j = 1 ∑ m / d [ g cd( i , j ) = 1 ] = d ∑ d k i = 1 ∑ n / d j = 1 ∑ m / d g ∣ i , g ∣ j ∑ μ ( g ) = d = 1 ∑ n d k g = 1 ∑ n / d μ ( g ) ⌊ d g n ⌋ ⌊ d g m ⌋ = h = 1 ∑ n ⌊ h n ⌋ ⌊ h m ⌋ d ∣ h ∑ d k μ ( d h )
观察到
f ( x ) = ∑ d ∣ x d k μ ( x d ) f(x)=\sum_{d|x}d^k\mu\left(\frac{x}{d}\right)
f ( x ) = d ∣ x ∑ d k μ ( d x )
是积性函数。于是我们用线性筛、快速幂预处理出它。然后整除分块处理每个询问即可。
P3768 简单的数学题
∑ i = 1 n ∑ j = 1 n i j gcd ( i , j ) = ∑ d d ∑ i = 1 n ∑ j = 1 n i j [ gcd ( i , j ) = d ] = ∑ d d 3 ∑ i = 1 n / d ∑ j = 1 n / d i j [ g c d ( i , j ) = 1 ] = ∑ d d 3 ∑ i = 1 n / d ∑ j = 1 n / d i j ∑ g ∣ i , g ∣ j μ ( g ) = ∑ d = 1 n d 3 ∑ g g 2 μ ( g ) ∑ i = 1 n / d g i ∑ j = 1 n / d g j \begin{aligned}
\sum_{i=1}^{n}\sum_{j=1}^{n}ij\gcd(i,j) & = \sum_{d}d\sum_{i=1}^{n}\sum_{j=1}^{n}ij[\gcd(i,j)=d] \\
& = \sum_{d}d^3\sum_{i=1}^{n/d}\sum_{j=1}^{n/d}ij[gcd(i,j)=1] \\
& = \sum_{d}d^3\sum_{i=1}^{n/d}\sum_{j=1}^{n/d}ij\sum_{g|i,g|j}\mu(g) \\
& = \sum_{d=1}^{n}d^3\sum_{g}g^2\mu(g)\sum_{i=1}^{n/dg}i\sum_{j=1}^{n/dg}j
\end{aligned}
i = 1 ∑ n j = 1 ∑ n i j g cd( i , j ) = d ∑ d i = 1 ∑ n j = 1 ∑ n i j [ g cd( i , j ) = d ] = d ∑ d 3 i = 1 ∑ n / d j = 1 ∑ n / d i j [ g c d ( i , j ) = 1 ] = d ∑ d 3 i = 1 ∑ n / d j = 1 ∑ n / d i j g ∣ i , g ∣ j ∑ μ ( g ) = d = 1 ∑ n d 3 g ∑ g 2 μ ( g ) i = 1 ∑ n / d g i j = 1 ∑ n / d g j
记 S ( x ) = ∑ i = 1 x i ∑ j = 1 x j = x 2 ( x + 1 ) 2 4 S(x)=\sum_{i=1}^{x}i\sum_{j=1}^{x}j = \frac{x^2(x+1)^2}{4} S ( x ) = ∑ i = 1 x i ∑ j = 1 x j = 4 x 2 ( x + 1 ) 2 。
∑ d = 1 n d 3 ∑ g g 2 μ ( g ) ∑ i = 1 n / d g i ∑ j = 1 n / d g j = ∑ d = 1 n d 3 ∑ g g 2 μ ( g ) S ( ⌊ n d g ⌋ ) = ∑ k = 1 n S ( ⌊ n k ⌋ ) ∑ d ∣ k d 3 ( k d ) 2 μ ( k d ) = ∑ k = 1 n k 2 S ( ⌊ n k ⌋ ) ∑ d ∣ k d μ ( k d ) = ∑ k = 1 n k 2 S ( ⌊ n k ⌋ ) φ ( k ) \begin{aligned}
\sum_{d=1}^{n}d^3\sum_{g}g^2\mu(g)\sum_{i=1}^{n/dg}i\sum_{j=1}^{n/dg}j & = \sum_{d=1}^{n}d^3\sum_{g}g^2\mu(g)S(\left\lfloor\frac{n}{dg}\right\rfloor) \\
& = \sum_{k=1}^{n}S(\left\lfloor\frac{n}{k}\right\rfloor)\sum_{d|k}d^3\left(\frac{k}{d}\right)^2\mu\left(\frac{k}{d}\right) \\
& = \sum_{k=1}^{n} k^2 S(\left\lfloor\frac{n}{k}\right\rfloor)\sum_{d|k}d\mu\left(\frac{k}{d}\right) \\
& = \sum_{k=1}^{n} k^2 S(\left\lfloor\frac{n}{k}\right\rfloor) \varphi(k)
\end{aligned}
d = 1 ∑ n d 3 g ∑ g 2 μ ( g ) i = 1 ∑ n / d g i j = 1 ∑ n / d g j = d = 1 ∑ n d 3 g ∑ g 2 μ ( g ) S ( ⌊ d g n ⌋ ) = k = 1 ∑ n S ( ⌊ k n ⌋ ) d ∣ k ∑ d 3 ( d k ) 2 μ ( d k ) = k = 1 ∑ n k 2 S ( ⌊ k n ⌋ ) d ∣ k ∑ d μ ( d k ) = k = 1 ∑ n k 2 S ( ⌊ k n ⌋ ) φ ( k )
注意到
f ( x ) = x 2 φ ( x ) f(x)=x^2\varphi(x)
f ( x ) = x 2 φ ( x )
是积性函数。
观察数据范围,线性筛 f f f 不行,需要杜教筛。
对于杜教筛:令 f ( x ) = x 2 φ ( x ) f(x)=x^2\varphi(x) f ( x ) = x 2 φ ( x ) ,s ( x ) = ∑ i = 1 x f ( x ) s(x)=\sum_{i=1}^{x}f(x) s ( x ) = ∑ i = 1 x f ( x )
∑ i = 1 n ( f ∗ g ) ( i ) = ∑ i = 1 n ∑ d ∣ i g ( d ) f ( i / d ) = ∑ d = 1 n g ( d ) ∑ i = 1 n / d f ( i ) = ∑ d = 1 n g ( d ) s ( n / d ) ∴ s ( n ) g ( 1 ) = ∑ i = 1 n ( f ∗ g ) ( i ) − ∑ d = 2 n g ( d ) s ( n / d ) \begin{aligned}
\sum_{i=1}^n (f*g)(i) & = \sum_{i=1}^{n}\sum_{d|i} g(d)f(i/d) \\
& = \sum_{d=1}^{n} g(d)\sum_{i=1}^{n/d}f(i)\\
& = \sum_{d=1}^{n} g(d)s(n/d) \\
\therefore s(n)g(1) & = \sum_{i=1}^{n}(f*g)(i)-\sum_{d=2}^{n}g(d)s(n/d)\\
\end{aligned}
i = 1 ∑ n ( f ∗ g ) ( i ) ∴ s ( n ) g ( 1 ) = i = 1 ∑ n d ∣ i ∑ g ( d ) f ( i / d ) = d = 1 ∑ n g ( d ) i = 1 ∑ n / d f ( i ) = d = 1 ∑ n g ( d ) s ( n / d ) = i = 1 ∑ n ( f ∗ g ) ( i ) − d = 2 ∑ n g ( d ) s ( n / d )
令 g ( x ) = x 2 g(x)=x^2 g ( x ) = x 2 ,则
( f ∗ g ) ( x ) = ∑ d ∣ x f ( d ) g ( x / d ) = x 2 ∑ d ∣ x φ ( d ) = x 3 ∴ ∑ i = 1 x ( f ∗ g ) ( i ) = x 2 ( x + 1 ) 2 4 \begin{aligned}
(f*g)(x) & = \sum_{d|x}f(d)g(x/d)\\
& = x^2\sum_{d|x}\varphi(d) \\
& = x^3 \\
\therefore \sum_{i=1}^{x}(f*g)(i)&=\frac{x^2(x+1)^2}{4}
\end{aligned}
( f ∗ g ) ( x ) ∴ i = 1 ∑ x ( f ∗ g ) ( i ) = d ∣ x ∑ f ( d ) g ( x / d ) = x 2 d ∣ x ∑ φ ( d ) = x 3 = 4 x 2 ( x + 1 ) 2
P3704 [SDOI2017]数字表格
不妨设 n ≤ m n\le m n ≤ m 。
∏ i = 1 n ∏ j = 1 m f gcd ( i , j ) = ∏ d f d ∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = d ] = ∏ d = 1 n f d ∑ i = 1 n / d ∑ j = 1 m / d [ gcd ( i , j ) = 1 ] ∑ i = 1 n / d ∑ j = 1 m / d [ gcd ( i , j ) = 1 ] = ∑ i = 1 n / d ∑ j = 1 m / d ∑ g ∣ i , g ∣ j μ ( g ) = ∑ g = 1 n / d μ ( g ) ⌊ n g d ⌋ ⌊ m g d ⌋ ∴ ∏ i = 1 n ∏ j = 1 m f gcd ( i , j ) = ∏ d = 1 n f d ∑ g = 1 n / d μ ( g ) ⌊ n g d ⌋ ⌊ m g d ⌋ = ∏ k = 1 n ∏ d ∣ k f d ⌊ n k ⌋ ⌊ m k ⌋ μ ( ⌊ k d ⌋ ) = ∏ k = 1 n ( ∏ d ∣ k f d μ ( ⌊ k d ⌋ ) ) ⌊ n k ⌋ ⌊ m k ⌋ \begin{aligned}
\prod_{i=1}^{n}\prod_{j=1}^{m}f_{\gcd(i,j)} & = \prod_{d}f_d^{\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=d]} \\
& = \prod_{d=1}^{n}f_{d}^{\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}[\gcd(i,j)=1]} \\
\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}[\gcd(i,j)=1] & = \sum_{i=1}^{n/d}\sum_{j=1}^{m/d}\sum_{g|i,g|j}\mu(g) \\
& = \sum_{g=1}^{n/d}\mu(g)\left\lfloor\frac{n}{gd}\right\rfloor\left\lfloor\frac{m}{gd}\right\rfloor \\
\therefore \prod_{i=1}^{n}\prod_{j=1}^{m}f_{\gcd(i,j)} & = \prod_{d=1}^{n}f_d^{\sum_{g=1}^{n/d}\mu(g)\left\lfloor\frac{n}{gd}\right\rfloor\left\lfloor\frac{m}{gd}\right\rfloor} \\
& = \prod_{k=1}^{n}\prod_{d|k}f_d^{\left\lfloor\frac{n}{k}\right\rfloor\left\lfloor\frac{m}{k}\right\rfloor\mu(\left\lfloor\frac{k}{d}\right\rfloor)} \\
& = \prod_{k=1}^{n}\left(\prod_{d|k}f_d^{\mu(\left\lfloor\frac{k}{d}\right\rfloor)}\right)^{\left\lfloor\frac{n}{k}\right\rfloor\left\lfloor\frac{m}{k}\right\rfloor}
\end{aligned}
i = 1 ∏ n j = 1 ∏ m f g c d ( i , j ) i = 1 ∑ n / d j = 1 ∑ m / d [ g cd( i , j ) = 1 ] ∴ i = 1 ∏ n j = 1 ∏ m f g c d ( i , j ) = d ∏ f d ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = d ] = d = 1 ∏ n f d ∑ i = 1 n / d ∑ j = 1 m / d [ g c d ( i , j ) = 1 ] = i = 1 ∑ n / d j = 1 ∑ m / d g ∣ i , g ∣ j ∑ μ ( g ) = g = 1 ∑ n / d μ ( g ) ⌊ g d n ⌋ ⌊ g d m ⌋ = d = 1 ∏ n f d ∑ g = 1 n / d μ ( g ) ⌊ g d n ⌋ ⌊ g d m ⌋ = k = 1 ∏ n d ∣ k ∏ f d ⌊ k n ⌋ ⌊ k m ⌋ μ ( ⌊ d k ⌋ ) = k = 1 ∏ n ⎝ ⎛ d ∣ k ∏ f d μ ( ⌊ d k ⌋ ) ⎠ ⎞ ⌊ k n ⌋ ⌊ k m ⌋
用类似埃氏筛的方法预处理
g ( x ) = ∏ d ∣ x f d μ ( ⌊ x d ⌋ ) g(x)=\prod_{d|x}f_d^{\mu(\left\lfloor\frac{x}{d}\right\rfloor)}
g ( x ) = d ∣ x ∏ f d μ ( ⌊ d x ⌋ )
即可。询问用整除分块。
注意对指数 ⌊ n k ⌋ ⌊ m k ⌋ \left\lfloor\frac{n}{k}\right\rfloor\left\lfloor\frac{m}{k}\right\rfloor ⌊ k n ⌋ ⌊ k m ⌋ 取模时使用费马小定理 a p − 1 ≡ 1 ( m o d p ) a^{p-1}\equiv 1\ (\bmod\ p) a p − 1 ≡ 1 ( m o d p ) 。