ProjectEuler_做题记录
简单记录一下。
problem 441 The inverse summation of coprime couples
神仙题。考虑答案为:
\[\begin{array}{c} S(n) & = & \sum_{i = 1} ^ n \sum_{p = 1} ^ i \sum_{q = p + 1} ^ i \frac {1}{pq}[p + q \geq i][gcd(p, q) = 1] \\ & = & \sum_{i = 1} ^ n \sum_{p = 1} ^ i \sum_{q = p + 1} ^ i \frac {1}{pq}[p + q \geq i] \sum_{d|gcd(p, q)} \mu(d) \\ & = & \sum_{i = 1} ^ n \sum_{d = 1} ^ n \mu(d) \sum_{p = 1} ^ {\lfloor i/d \rfloor} \sum_{q = 1} ^ {\lfloor i/d \rfloor} \frac {1}{pqd ^ 2}[pd + qd \geq i]\\ & = & \sum_{i = 1} ^ n \sum_{d = 1} ^ n \frac {\mu(d)}{d ^ 2} \sum_{p = 1} ^ {\lfloor i/d \rfloor} \sum_{q = 1} ^ {\lfloor i/d \rfloor} \frac {1}{pq}[p + q \geq \lfloor i/d \rfloor] \\ & - & \frac {1}{pq}[p + q = \lfloor i/d \rfloor][i \ mod \ d = 0] \\ \end{array}\]
我们考虑构造三个函数:
\[\begin{array}{c} F(n) & = & \sum_{i = 1} ^ n \frac {1}{i} \\ G(n) & = & \sum_{i = 1} ^ n \sum_{j = i + 1} ^ n \frac {1}{ij} [i + j = n] \\ T(n) & = & \sum_{i = 1} ^ n \sum_{j = i + 1} ^ n \frac {1}{ij} [i + j \geq n] \\ \end{array}\]
事实上,我们可以解出这三个函数的递归式,然后做到\(O(N)\)预处理。
然后最后的式子会变成:
\[S(n) = \sum_{d = 1} ^ n \frac {\mu(d)}{d ^ 2} \sum_{i = d} ^ n(T(\lfloor i/d \rfloor) - G(\lfloor i/d \rfloor)[i \ mod \ d \neq 0]) \]
然后就可以根据调和级数做到\(O(n \ lnn)\)。
problem 530 GCD of Divisors
由于直接化简比较困难,考虑将整个答案一起化简:
\[\begin{array}{c} ans & = & \sum_{i = 1} ^ n \sum_{d \mid i} gcd(d, \frac {i}{d}) \\ & = & \sum_{g = 1} ^ n g \sum_{i = 1} ^ {n} \sum_{d \mid i} [gcd(d, \frac {i}{d}) = g](先枚举gcd的套路)\\ & = & \sum_{g = 1} ^ n g \sum_{i = 1} ^ {\lfloor n/g^2 \rfloor} \sum_{d \mid i} [gcd(d, \frac {i}{d}) = 1](设i = \frac {i}{g ^ 2}, d = \frac {d}{g})\\ & = & \sum_{g = 1} ^ n g \sum_{i = 1} ^ {\lfloor n/g^2 \rfloor} \sum_{d \mid i} \sum_{t \mid gcd(d, \frac {i}{d})} \mu(t) \\ & = & \sum_{g = 1} ^ n g \sum_{t = 1} ^ n \mu(t) \sum_{i = 1} ^ {\lfloor n/(gt) ^ 2 \rfloor} \sum_{d \mid i} 1 (设i = \frac {i}{t ^ 2}, d = \frac {d}{t},也是套路)\\ & = & \sum_{g = 1} ^ n g \sum_{t = 1} ^ n \mu(t) \sum_{i = 1} ^ {\lfloor n/(gt) ^ 2 \rfloor} \sigma_0(i)(\sigma_0(i)表示i的因子个数)\\ &!&(下一步是个新套路,构造\phi的卷积形式\phi = \mu * 1) \\ & = & \sum_{k = 1} ^ {\sqrt n} \sum_{g \mid k} g\mu(\frac {k}{g}) \sum_{i = 1} ^ {\lfloor n/k ^ 2 \rfloor} \sigma_0(i)(k = gt,注意k的取值只需要到\sqrt n)\\ & = & \sum_{k = 1} ^ {\sqrt n} \phi(k) \sum_{i = 1} ^ {\lfloor n/k ^ 2 \rfloor} \sigma_0(i)\\ &!& (最后套用\sum_{i = 1} ^ {m} \sigma_0(i) = \sum_{i = 1} ^ m \lfloor \frac {m}{i} \rfloor)\\ & = & \sum_{k = 1} ^ {\sqrt n} \phi(k) \sum_{i = 1} ^ {\lfloor n/k ^ 2 \rfloor} \lfloor \frac {\lfloor n/k ^ 2 \rfloor}{i} \rfloor \\ \end{array}\]
对后面部分使用除法分块,复杂度就是\(\sum_{i = 1} ^ {\sqrt n} O(\sqrt \frac {n}{i ^ 2}) = \sum_{i = 1} ^ {\sqrt n} O(\frac {\sqrt n}{i}) = O(\sqrt n \ ln \sqrt n)\)