project euler做题记录
Posted whc200305
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了project euler做题记录相关的知识,希望对你有一定的参考价值。
ProjectEuler_做题记录
简单记录一下。
problem 441 The inverse summation of coprime couples
神仙题。考虑答案为:
[egin{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
floor} sum_{q = 1} ^ {lfloor i/d
floor} 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
floor} sum_{q = 1} ^ {lfloor i/d
floor} frac {1}{pq}[p + q geq lfloor i/d
floor] \ & - & frac {1}{pq}[p + q = lfloor i/d
floor][i mod d = 0] \end{array}]
我们考虑构造三个函数:
[egin{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
floor) - G(lfloor i/d
floor)[i mod d
eq 0])
]
然后就可以根据调和级数做到(O(n lnn))。
problem 530 GCD of Divisors
由于直接化简比较困难,考虑将整个答案一起化简:
[egin{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
floor} 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
floor} 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
floor} 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
floor} sigma_0(i)(sigma_0(i)表示i的因子个数)&!&(下一步是个新套路,构造phi的卷积形式phi = mu * 1) & = & sum_{k = 1} ^ {sqrt n} sum_{g mid k} gmu(frac {k}{g}) sum_{i = 1} ^ {lfloor n/k ^ 2
floor} sigma_0(i)(k = gt,注意k的取值只需要到sqrt n)& = & sum_{k = 1} ^ {sqrt n} phi(k) sum_{i = 1} ^ {lfloor n/k ^ 2
floor} sigma_0(i)&!& (最后套用sum_{i = 1} ^ {m} sigma_0(i) = sum_{i = 1} ^ m lfloor frac {m}{i}
floor)& = & sum_{k = 1} ^ {sqrt n} phi(k) sum_{i = 1} ^ {lfloor n/k ^ 2
floor} lfloor frac {lfloor n/k ^ 2
floor}{i}
floor \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))
以上是关于project euler做题记录的主要内容,如果未能解决你的问题,请参考以下文章