题面
求∑ k = a b ∑ i = 1 k ∑ j = 1 i [ l c m ( i , j ) = = k ] largesum_{k=a}^bsum_{i=1}^ksum_{j=1}^i[lcm(i,j)==k] k = a ∑ b i = 1 ∑ k j = 1 ∑ i [ l c m ( i , j ) = = k ]
1 < = a < = b < = 1 0 11 1<=a<=b<=10^{11} 1 < = a < = b < = 1 0 1 1
题目分析
令f ( n ) = ∑ i = 1 n ∑ j = 1 i [ l c m ( i , j ) = = n ] large f(n)=sum_{i=1}^nsum_{j=1}^i[lcm(i,j)==n] f ( n ) = ∑ i = 1 n ∑ j = 1 i [ l c m ( i , j ) = = n ]
则A n s = ∑ i = a b f ( i ) large Ans=sum_{i=a}^bf(i) A n s = ∑ i = a b f ( i )
f ( n ) = ∑ i = 1 n ∑ j = 1 i [ i ⋅ j = = n ⋅ ( i , j ) ] = ∑ d ∣ n ∑ d ∣ i ∑ d ∣ j , j < = i [ i ⋅ j = = n d & & ( i d , j d ) = = 1 ] = ∑ d ∣ n ∑ i = 1 n d ∑ j = 1 i [ i j = = n d & & ( i , j ) = = 1 ] = ∑ d ∣ n ∑ i = 1 d ∑ j = 1 i [ i j = = d & & ( i , j ) = = 1 ] = ∑ d ∣ n h ( d ) large f(n)=sum_{i=1}^nsum_{j=1}^i[icdot j==ncdot (i,j)]\=sum_{d|n}sum_{d|i}sum_{d|j,j<=i}[icdot j==nd~&&~(frac id,frac jd)==1]\=sum_{d|n}sum_{i=1}^{frac nd}sum_{j=1}^i[ij==frac nd~&&~(i,j)==1]\=sum_{d|n}sum_{i=1}^dsum_{j=1}^i[ij==d~&&~(i,j)==1]\=sum_{d|n}h(d) f ( n ) = i = 1 ∑ n j = 1 ∑ i [ i ⋅ j = = n ⋅ ( i , j ) ] = d ∣ n ∑ d ∣ i ∑ d ∣ j , j < = i ∑ [ i ⋅ j = = n d & & ( d i , d j ) = = 1 ] = d ∣ n ∑ i = 1 ∑ d n j = 1 ∑ i [ i j = = d n & & ( i , j ) = = 1 ] = d ∣ n ∑ i = 1 ∑ d j = 1 ∑ i [ i j = = d & & ( i , j ) = = 1 ] = d ∣ n ∑ h ( d )
此处h ( d ) h(d) h ( d ) 表示小于等于d d d 中,满足两个数互质且乘积为d d d 的无序数对的个数,显然
h ( d ) = { 1 , d = 1 2 ω ( d ) − 1 , d = ∏ i = 1 ω ( d ) p i a i large h(d)=left{
egin{aligned}
&1~,~d=1\
&2^{omega(d)-1}~,~d=prod_{i=1}^{omega(d)}p_i^{a_i}\
end{aligned}
ight. h ( d ) = ⎩ ⎪ ⎪ ⎪ ⎪ ⎨ ⎪ ⎪ ⎪ ⎪ ⎧ 1 , d = 1 2 ω ( d ) − 1 , d = i = 1 ∏ ω ( d ) p i a i 其中ω ( d ) largeomega(d) ω ( d ) 表示d的质因子个数
相当于把d的质因数分成两部分,所以就每个质因数选或不选,又因为是无序数对,所以除以2,也可以写为以下形式
h ( d ) = 2 ω ( d ) + [ d = = 1 ] 2 large h(d)=frac{2^{omega(d)}+[d==1]}2 h ( d ) = 2 2 ω ( d ) + [ d = = 1 ]
有没有发现十分类似某个等式,记与n n n 互质的数的和为a ( n ) a(n) a ( n ) (随便选的字母),则
a ( n ) = φ ( n ) n + [ n = = 1 ] 2 a(n)=frac{varphi(n)n+[n==1]}2 a ( n ) = 2 φ ( n ) n + [ n = = 1 ]
回到这道题,有f ( n ) = ∑ d ∣ n h ( d ) = ∑ d ∣ n 2 ω ( d ) + [ d = = 1 ] 2 = 1 + ∑ d ∣ n 2 ω ( d ) 2 large f(n)=sum_{d|n}h(d)=sum_{d|n}frac{2^{omega(d)}+[d==1]}2\=frac{1+sum_{d|n}2^{omega(d)}}2 f ( n ) = d ∣ n ∑ h ( d ) = d ∣ n ∑ 2 2 ω ( d ) + [ d = = 1 ] = 2 1 + ∑ d ∣ n 2 ω ( d )
然后我们又发现其实2 ω ( d ) large2^{omega(d)} 2 ω ( d ) 是每个质因数选或不选的方案数,及d d d 的无平方因子的约数的个数,所以2 ω ( d ) = ∑ k ∣ d ∣ μ ( k ) ∣ large 2^{omega(d)}=sum_{k|d}|mu(k)| 2 ω ( d ) = k ∣ d ∑ ∣ μ ( k ) ∣ 根据μ mu μ 函数的定义,我们知道只有无平方因子数的函数值才为1或-1,所以加上绝对值就成了计数
∴ ∑ i = 1 n f ( i ) = n + ∑ i = 1 n ∑ d ∣ i 2 ω ( d ) 2 = n + ∑ i = 1 n ∑ d ∣ i ∑ k ∣ d ∣ μ ( k ) ∣ 2 large herefore sum_{i=1}^nf(i)=frac{n+sum_{i=1}^nsum_{d|i}2^{omega(d)}}2=frac{n+sum_{i=1}^nsum_{d|i}sum_{k|d}|mu(k)|}2 ∴ i = 1 ∑ n f ( i ) = 2 n + ∑ i = 1 n ∑ d ∣ i 2 ω ( d ) = 2 n + ∑ i = 1 n ∑ d ∣ i ∑ k ∣ d ∣ μ ( k ) ∣
∵ ∑ i = 1 n ∑ d ∣ i ∑ k ∣ d ∣ μ ( k ) ∣ = ∑ k = 1 n ∣ μ ( k ) ∣ ∑ k ∣ d ∑ d ∣ i 1 = ∑ k = 1 n ∣ μ ( k ) ∣ ∑ k ∣ d ⌊ n d ⌋ = ∑ k = 1 n ∣ μ ( k ) ∣ ∑ d = 1 ⌊ n k ⌋ ⌊ n d k ⌋ = ∑ k = 1 n ∣ μ ( k ) ∣ ∑ d = 1 ⌊ n k ⌋ ⌊ ⌊ n k ⌋ d ⌋ = ∑ k = 1 n ∣ μ ( k ) ∣ ∑ d = 1 ⌊ n k ⌋ ⌊ ⌊ n k ⌋ d ⌋ largeecause sum_{i=1}^nsum_{d|i}sum_{k|d}|mu(k)|=sum_{k=1}^n|mu(k)|sum_{k|d}sum_{d|i}1\=sum_{k=1}^n|mu(k)|sum_{k|d}lfloorfrac nd
floor\=sum_{k=1}^n|mu(k)|sum_{d=1}^{lfloorfrac nk
floor}lfloorfrac n{dk}
floor\=sum_{k=1}^n|mu(k)|sum_{d=1}^{lfloorfrac nk
floor}lfloorfrac {lfloorfrac nk
floor}d
floor\=sum_{k=1}^n|mu(k)|sum_{d=1}^{lfloorfrac nk
floor}lfloorfrac {lfloorfrac nk
floor}d
floor ∵ i = 1 ∑ n d ∣ i ∑ k ∣ d ∑ ∣ μ ( k ) ∣ = k = 1 ∑ n ∣ μ ( k ) ∣ k ∣ d ∑ d ∣ i ∑ 1 = k = 1 ∑ n ∣ μ ( k ) ∣ k ∣ d ∑ ⌊ d n ⌋ = k = 1 ∑ n ∣ μ ( k ) ∣ d = 1 ∑ ⌊ k n ⌋ ⌊ d k n ⌋ = k = 1 ∑ n ∣ μ ( k ) ∣ d = 1 ∑ ⌊ k n ⌋ ⌊ d ⌊ k n ⌋ ⌋ = k = 1 ∑ n ∣ μ ( k ) ∣ d = 1 ∑ ⌊ k n ⌋ ⌊ d ⌊ k n ⌋ ⌋
先看第二个∑ largesum ∑ ,对于某一个⌊ n k ⌋ large{lfloorfrac nk
floor} ⌊ k n ⌋ 的取值,把它记作N N N ,就以N N N 的范围做整除分块优化,Θ ( N ) largeTheta(sqrt N) Θ ( N ) 的时间复杂度,那么外层还有一个求和,于是在外面也套一层整除分块优化,预处理出前n 2 3 large n^{frac 23} n 3 2 后时间复杂度为Θ ( n 2 3 ) largeTheta(n^{frac23}) Θ ( n 3 2 )
此处预处理为线性筛,考虑变换,∑ i = 1 n ⌊ n i ⌋ largesum_{i=1}^nlarge{lfloorfrac ni
floor} ∑ i = 1 n ⌊ i n ⌋ 实际可看作枚举i i i 后看n n n 以内有多少个数能被i i i 整除,这不就是∑ i = 1 n σ 0 ( i ) largesum_{i=1}^nsigma_0(i) ∑ i = 1 n σ 0 ( i ) 吗?(这个函数表示i的约数个数 )
于是我们只需要筛出约数个数在累加就行了,线性筛时存一下当前数的最小质因子的次数就可以愉快的线性筛了
由于在外面一层套上了整除分块优化,则需要求出∣ μ ( k ) ∣ large |mu(k)| ∣ μ ( k ) ∣ 的前缀和,也就是n n n 以内的无平方因子数
这里处理无平方因子数时用容斥原理,有
∑ i = 1 n ∣ μ ( i ) ∣ = ∑ i = 1 n μ ( i ) ⋅ ⌊ n i 2 ⌋ largesum_{i=1}^n|mu(i)|=sum_{i=1}^{sqrt n}mu(i)cdotlfloorfrac n{i^2}
floor i = 1 ∑ n ∣ μ ( i ) ∣ = i = 1 ∑ n μ ( i ) ⋅ ⌊ i 2 n ⌋ 想想μ mu μ 函数的定义,这个容斥还是比较好理解的
Θ ( n ) large Theta(sqrt n) Θ ( n ) 可处理出来
其实这道题用的是[SPOJ] DIVCNT2 - Counting Divisors (square) 一模一样的方法(我后面的分析都是直接粘的233)
但是奈何这道题T M color{white}TM T M 的卡内存!(各种预处理+卡内存我只能做到6015ms>6000ms T了…)
只能想想别的办法(也许有些同学掌握特殊的卡常技巧能过A掉吧)
.
.
.
.
.
好的。
正文开始
那么我就要开始略讲一点了
定义F ( i ) = ∑ x = 1 i ∑ y = 1 x [ x y ( x , y ) = = i ] large F(i)=sum_{x=1}^isum_{y=1}^x[frac{xy}{(x,y)}==i] F ( i ) = ∑ x = 1 i ∑ y = 1 x [ ( x , y ) x y = = i ]
则A n s = ∑ i = 1 b F ( i ) − ∑ i = 1 a − 1 F ( i ) large Ans=sum_{i=1}^bF(i)-sum_{i=1}^{a-1}F(i) A n s = ∑ i = 1 b F ( i ) − ∑ i = 1 a − 1 F ( i )
考虑∑ i = 1 n F ( i ) sum_{i=1}^nF(i) ∑ i = 1 n F ( i ) 怎么求
设( x , y ) = r , x = a r , y = b r (x,y)=r,x=ar,y=br ( x , y ) = r , x = a r , y = b r ,则
∑ i = 1 n F ( i ) = ∑ x = 1 n ∑ y = 1 x [ x y r ≤ n ] = ∑ a ∑ b ∑ r [ a ≤ b ] [ ( a , b ) = = 1 ] [ a b r ≤ n ] large sum_{i=1}^nF(i)=sum_{x=1}^nsum_{y=1}^x[frac {xy}rle n]\=sum_asum_bsum_r[ale b][(a,b)==1][abrle n] i = 1 ∑ n F ( i ) = x = 1 ∑ n y = 1 ∑ x [ r x y ≤ n ] = a ∑ b ∑ r ∑ [ a ≤ b ] [ ( a , b ) = = 1 ] [ a b r ≤ n ] 反演一下得到∑ i = 1 n F ( i ) = ∑ d = 1 n μ ( d ) ∑ a ∑ b ∑ r [ a ≤ b ] [ a b r ≤ ⌊ n d 2 ⌋ ] large sum_{i=1}^nF(i)=sum_{d=1}^{sqrt n}mu(d)sum_asum_bsum_r[ale b][abrle lfloorfrac n{d^2}
floor] i = 1 ∑ n F ( i ) = d = 1 ∑ n μ ( d ) a ∑ b ∑ r ∑ [ a ≤ b ] [ a b r ≤ ⌊ d 2 n ⌋ ] 不考虑[ a ≤ b ] [ale b] [ a ≤ b ] 的话就是a , b , r a,b,r a , b , r 三个数的乘积的统计,做法为统计x ≤ y ≤ z & & x y z ≤ N xle yle z~&&~xyzle N x ≤ y ≤ z & & x y z ≤ N 的( x , y , z ) (x,y,z) ( x , y , z ) 数对的数量
分别考虑三个数不相等/两个数相等/三个数相等的情况再分别乘上排列系数
由于这样的枚举只需要枚举x ≤ N 1 3 large xle N^{frac 13} x ≤ N 3 1 ,在枚举x ≤ y ≤ N x large xle yle frac Nx x ≤ y ≤ x N ,然后Θ ( 1 ) Theta(1) Θ ( 1 ) 统计z z z 的个数
[ a ≤ b ] [ale b] [ a ≤ b ] 比较讨厌,假设( a , b ) (a,b) ( a , b ) 与( b , a ) (b,a) ( b , a ) 被统计两次,因为有a = b a=b a = b 的情况不能直接除以2 2 2
回到原问题,当a = b a=b a = b 时,由于( a , b ) = 1 (a,b)=1 ( a , b ) = 1 ,所以a = b = 1 a=b=1 a = b = 1 ,此时r r r 有n n n 种取值,所以只需要在算出的结果加上n n n 再除以2 2 2 即可
参考自 51Nod 题解1楼
时间复杂度证明
设T ( n ) T(n) T ( n ) 表示统计满足x y z < = n xyz<=n x y z < = n 的( x , y , z ) (x,y,z) ( x , y , z ) 的三元组个数的时间复杂度
T ( n ) = Θ ( ∑ x = 1 n 1 3 ( n x − x ) ) = Θ ( n 2 3 ) large T(n)=Thetaleft(sum_{x=1}^{n^{frac 13}}(sqrt{frac nx}-x)
ight)=Theta(n^{frac 23}) T ( n ) = Θ ⎝ ⎜ ⎜ ⎛ x = 1 ∑ n 3 1 ( x n − x ) ⎠ ⎟ ⎟ ⎞ = Θ ( n 3 2 )
上面的− x -x − x 是枚举y y y 时从x + 1 x+1 x + 1 开始枚举,总时间复杂度= Θ ( ∑ d = 1 n T ( n d 2 ) ) = Θ ( ∑ d = 1 n T ( ( n d ) 2 ) ) = Θ ( ∑ d = 1 n ( n d ) 4 3 ) = Θ ( ∑ d = 1 n ( n 2 3 d 4 3 ) ) = Θ ( n 2 3 ∑ d = 1 n ( 1 d 4 3 ) ) large =Thetaleft(sum_{d=1}^{sqrt n}T(frac n{d^2})
ight)\=Thetaleft(sum_{d=1}^{sqrt n}T((frac{sqrt n}d)^2)
ight)\=Thetaleft(sum_{d=1}^{sqrt n}(frac{sqrt n}d)^{frac 43}
ight)\=Thetaleft(sum_{d=1}^{sqrt n}(frac{n^{frac 23}}{d^{frac 43}})
ight)\=Thetaleft(n^{frac 23}sum_{d=1}^{sqrt n}(frac{1}{d^{frac 43}})
ight) = Θ ⎝ ⎜ ⎛ d = 1 ∑ n T ( d 2 n ) ⎠ ⎟ ⎞ = Θ ⎝ ⎜ ⎛ d = 1 ∑ n T ( ( d n ) 2 ) ⎠ ⎟ ⎞ = Θ ⎝ ⎜ ⎛ d = 1 ∑ n ( d n ) 3 4 ⎠ ⎟ ⎞ = Θ ⎝ ⎜ ⎛ d = 1 ∑ n ( d 3 4 n 3 2 ) ⎠ ⎟ ⎞ = Θ ⎝ ⎜ ⎛ n 3 2 d = 1 ∑ n ( d 3 4 1 ) ⎠ ⎟ ⎞ 此处d d d 的枚举应该是离散的,我们将它看做连续的,由于n = n 1 2 sqrt n=n^{frac 12} n = n 2 1 那么将d d d 两两结合,有
∫ 1 n 1 4 ( 1 x 4 3 ) + ( 1 ( n 1 2 x 3 4 ) ) d x > = ∫ 1 n 1 4 2 ( 1 x 4 3 ( n 1 2 x 3 4 ) ) d x = ∫ 1 n 1 4 2 ( 1 n 1 2 ) d x largeint_{1}^{n^{frac 14}} (frac 1{x^{frac 43}})+(frac 1{(frac{n^{frac 12}}{x^{frac 34}})})dx\>=int_{1}^{n^{frac 14}} 2left(frac 1{x^{frac 43}{(frac{n^{frac 12}}{x^{frac 34}})}}
ight)dx\=int_{1}^{n^{frac 14}} 2left(frac 1{n^{frac 12}}
ight)dx ∫ 1 n 4 1 ( x 3 4 1 ) + ( ( x 4 3 n 2 1 ) 1 ) d x > = ∫ 1 n 4 1 2 ⎝ ⎜ ⎜ ⎛ x 3 4 ( x 4 3 n 2 1 ) 1 ⎠ ⎟ ⎟ ⎞ d x = ∫ 1 n 4 1 2 ( n 2 1 1 ) d x 于是我们又回到离散的,最多有n 1 2 2 frac{n^frac 12}2 2 n 2 1 对这样的关系,所以
∑ d = 1 n ( 1 d 4 3 ) = n 1 2 2 ⋅ 2 ( 1 n 1 2 ) = 1 large sum_{d=1}^{sqrt n}(frac{1}{d^{frac 43}})=frac{n^frac 12}2cdot2left(frac 1{n^{frac 12}}
ight)=1 d = 1 ∑ n ( d 3 4 1 ) = 2 n 2 1 ⋅ 2 ( n 2 1 1 ) = 1 所以总时间复杂度为Θ ( n 2 3 ∑ d = 1 n ( 1 d 4 3 ) ) = Θ ( n 2 3 ) large Thetaleft(n^{frac 23}sum_{d=1}^{sqrt n}(frac{1}{d^{frac 43}})
ight)=Theta(n^{frac 23}) Θ ⎝ ⎜ ⎛ n 3 2 d = 1 ∑ n ( d 3 4 1 ) ⎠ ⎟ ⎞ = Θ ( n 3 2 )
申明:以上的∫ int ∫ 符号纯属乱用,只是不能再用∑ sum ∑ 表示x x x 的取值可能不为整数 的和了
AC code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 316228 ;
int Cnt, Prime[ N] , mu[ N] ;
bool IsnotPrime[ N] ;
void init ( )
{
mu[ 1 ] = 1 ;
for ( int i = 2 ; i < N; ++ i)
{
if ( ! IsnotPrime[ i] )
Prime[ ++ Cnt] = i, mu[ i] = - 1 ;
for ( int j = 1 ; j <= Cnt && i * Prime[ j] < N; ++ j)
{
IsnotPrime[ i * Prime[ j] ] = 1 ;
if ( i % Prime[ j] == 0 ) { mu[ i * Prime[ j] ] = 0 ; break ; }
mu[ i * Prime[ j] ] = - mu[ i] ;
}
}
}
LL solve ( LL n)
{
LL ret = n;
for ( int d = 1 ; ( LL) d* d <= n; ++ d) if ( mu[ d] )
{
LL m = n/ ( ( LL) d* d) , s = 0 ;
for ( int a = 1 ; ( LL) a* a* a <= m; ++ a)
{
for ( int b = a+ 1 ; ( LL) a* b* b <= m; ++ b)
s + = ( m/ ( ( LL) a* b) - b) * 6 + 3 ;
s + = ( m/ ( ( LL) a* a) - a) * 3 + 1 ;
}
ret + = s * mu[ d] ;
}
return ret / 2 ;
}
int main ( )
{
LL a, b; init ( ) ;
scanf ( "%lld%lld" , & a, & b) ;
printf ( "%lld
" , solve ( b) - solve ( a- 1 ) ) ;
}
.
.
可写死我了