[ans=sum_{i=1}^nsum_{j=1}^nlcm(A_i,A_j)
]
[egin{aligned}
ans
& =sum_{i=1}^nsum_{j=1}^nlcm(A_i,A_j)
\ & =sum_{i=1}^nsum_{j=1}^nfrac{A_icdot A_j}{gcd(A_i,A_j)}
\ & =sum_{i=1}^nsum_{j=1}^nA_icdot A_jsum_{t=1}^{A_i}frac{1}{t}[gcd(A_i,A_j)=t]
\ & =sum_{t=1}^{A_m}frac{1}{t}sum_{i=1}^nsum_{j=1}^nA_icdot A_j[gcd(A_i,A_j)=t]
\ & =sum_{t=1}^{A_m}tsum_{i=1}^nsum_{j=1}^n[A_i mod; t=0][A_j mod;t=0]cdot frac{A_icdot A_j}{t^2}[gcd(A_i/t,A_j/t)=1]
\ & =sum_{t=1}^{A_m}tsum_{i=1}^nsum_{j=1}^n[A_i mod; t=0][A_j mod;t=0]cdot frac{A_icdot A_j}{t^2}sum_{d|gcd(A_i/t,A_j/t)}mu(d)
end{aligned}
]
打住,我们这些式子始终都在数组上进行,根本没有脱离数组,这导致我们根本没法进行数论变换
所以我们需要把数组转化到数上去
[1leq N leq 50000
]
[1leq A_i leq 50000
]
我们就可以根据这个式子
[sum_{i=1}^{n}sum_{j=1}^nlcm(i,j)
]
我们想想怎么加一个限制才能使答案不重不漏
数量!
我们记录一个数字x在数列中出现了多少次,记为数组c
所以我们现在要求的是
[egin{aligned}
ans=
& sum_{i=1}^nsum_{j=1}^n lcm(i,j) imes c_i imes c_j\
=& sum_{i=1}^nsum_{j=1}^n frac{i imes j imes c_i imes c_j}{gcd(i,j)} \
=& sum_{d=1}^nsum_{i=1}^{lfloor n/d
floor}sum_{j=1}^{lfloor n/d
floor}[gcd(i,j)=1]d imes i imes j imes c_{id} imes c_{jd} \
=& sum_{d=1}^nsum_{i=1}^{lfloor n/d
floor}sum_{j=1}^{lfloor n/d
floor}sum_{k|gcd(i,j)}mu(k) imes d imes i imes j imes c_{id} imes c_{jd} \
=& sum_{d=1}^nsum_{k=1}^{lfloor n/d
floor}sum_{i=1}^{lfloor n/kd
floor}sum_{j=1}^{lfloor n/kd
floor}mu(k) imes d imes i imes j imes k^2 imes c_{idk} imes c_{jdk} \
=& sum_{T=1}^{n}T imes(sum_{i=1}^{lfloor n/T
floor}i imes c_{iT})^2sum_{k|T}mu(k) imes k
end{aligned}
]
那么我们前面的可以进行预处理
时间复杂度
[O(ncdot ln(n))
]