题目大意:
求:
[sum_{i=1}^nsum_{j=1}^nsum_{k=1}^ngcd(i,j,k)
]
正文:
本题有两种方法,第一种方法用莫比乌斯反演,第二种方法是用到欧拉函数。
方法一:
可以枚举这个最大公约数来化简这个式子:
[egin{aligned}sum_{i=1}^nsum_{j=1}^nsum_{k=1}^ngcd(i,j,k)&=sum_{d=1}^{n}sum_{i=1}^nsum_{j=1}^nsum_{k=1}^nleft[gcd(i,j,k)==d
ight]d\&=sum_{d=1}^{n}dsum_{i=1}^{lfloorfrac{n}{d}
floor}sum_{j=1}^{lfloorfrac{n}{d}
floor}sum_{k=1}^{lfloorfrac{n}{d}
floor}left[gcd(i,j,k)==1
ight]\&=sum_{d=1}^{n}dsum_{i=1}^{lfloorfrac{n}{d}
floor}sum_{j=1}^{lfloorfrac{n}{d}
floor}sum_{k=1}^{lfloorfrac{n}{d}
floor}sum_{l|gcd(i,j,k)}mu(l)\&=sum_{d=1}^{n}dsum_{l=1}^{lfloorfrac{n}{d}
floor}mu(l)sum_{i|l}^{lfloorfrac{n}{d}
floor}sum_{j|l}^{lfloorfrac{n}{d}
floor}sum_{k|l}^{lfloorfrac{n}{d}
floor}1\&=sum_{d=1}^{n}dsum_{l=1}^{lfloorfrac{n}{d}
floor}mu(l)leftlfloorfrac{n}{dl}
ight
floor^3end{aligned}
]
然后就可以 (O(nsqrt{n})) 做了,虽然还有更优的做法,但这里不作介绍。
方法二:
欧拉函数更简单还更快。首先有 (sum_{d|n}varphi(d)=n),那么代入式子:
[egin{aligned}sum_{i=1}^nsum_{j=1}^nsum_{k=1}^ngcd(i,j,k)&=sum_{i=1}^nsum_{j=1}^nsum_{k=1}^nsum_{d|gcd(i,j,k)}varphi(d)\&=sum_{d=1}^{n}varphi(d)sum_{i=1}^{lfloorfrac{n}{d}
floor}sum_{j=1}^{lfloorfrac{n}{d}
floor}sum_{k=1}^{lfloorfrac{n}{d}
floor}1\&=sum_{d|gcd(i,j,k)}varphi(d)leftlfloorfrac{n}{d}
ight
floor^3end{aligned}
]
核心代码时间复杂度 (O(sqrt{n}))。