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/
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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/
Name |
---|
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