I have been trying to solve this problem for a while now but made no progress. Can anybody help with TRENDGCD — Trending GCD at spoj.it is related to mobius function. here is link to the problem https://www.spoj.com/problems/TRENDGCD/
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
I have been trying to solve this problem for a while now but made no progress. Can anybody help with TRENDGCD — Trending GCD at spoj.it is related to mobius function. here is link to the problem https://www.spoj.com/problems/TRENDGCD/
Название |
---|
Auto comment: topic has been updated by deepkamal (previous revision, new revision, compare).
Let $$$g(d)$$$ be the function such that $$$f(n) = \sum_{d|n} g(d)$$$.
Since $$$f(1) = 1$$$, this is guaranteed to exist. With some number theoretic knowledge, we know that $$$g(n) = \sum_{d|n}\mu(d)f(\frac{n}{d})$$$
Calculating the Dirichlet convolution of two functions up to $$$n$$$ takes $$$O(n\log(n))$$$ time and pre-computing the $$$\mu$$$ function takes $$$O(n)$$$ time using linear sieve. Hence we can get $$$g(n)$$$ in $$$O(n\log(n))$$$ time.
Now we rewrite the thing to be calculated:
Now it's solvable in $$$O(n)$$$ time per case.
Note that when $$$n$$$ is fixed, $$$\lfloor{n/d}\rfloor$$$ only takes up to $$$2\sqrt{n}$$$ distinct values and is non-increasing as $$$d$$$ grows. Therefore, if you pre-calculate the prefix sum of $$$g(d)d^2$$$, you will be able to calculate the contribution for each segment in $$$O(1)$$$ time. This results in a $$$O(\sqrt{n})$$$ solution.
Here is my code
P.S.: Given $$$d\le n$$$, the largest number $$$r$$$ such that $$$ \lfloor{n/d}\rfloor = \lfloor{n/r}\rfloor$$$ is $$$r = \lfloor{n/(\lfloor{n/d}\rfloor)}\rfloor$$$.
thank you so much