How to solve this problem using matrix exponentiation. The recurrence relation is :
f(n, k, 0) = 2 * f(n - 1, k, 1) + f(n - 1, k, 0)
f(n, k, 1) = f(n - 1, k, 1) + f(n - 1, k, 0)
1 < n < 1e9
1 < k < 1e3
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
5 | -is-this-fft- | 158 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
9 | awoo | 152 |
10 | luogu_official | 151 |
How to solve this problem using matrix exponentiation. The recurrence relation is :
f(n, k, 0) = 2 * f(n - 1, k, 1) + f(n - 1, k, 0)
f(n, k, 1) = f(n - 1, k, 1) + f(n - 1, k, 0)
1 < n < 1e9
1 < k < 1e3
Name |
---|
Matrix exponentiation is and it can be optimized to , but it's too slow and may still get TLE.
I used a O(k) solution.
Can you please explain your idea. I couldn't understand the editorial.
Can you provide a link to your solution or maybe tell your codechef handle so that I can look at your submission ? It will be helpful.
https://www.codechef.com/viewsolution/20839640
Thanks :)
The O(K) solution has been mentioned in the editorial.
I solved it by
Lagrange Interpolation
Let f(n, k) denote the answer then we have the recurrence
Now $f(n, 1) = 2n $ , which is linear, which implies that f(n, 2) must be quadratic, which implies that f(n, 3) must be cubic and so on..
So for fixed k, f(n, k) will be a kth degree polynomial in n, and therefore i precomputed f(n, k) for n, k upto 2000 and then I answered each test case in O(n) using Lagrange Interpolation.
So overall time complexity O(K2 + TN)
My solution for reference
I have heard of this term first time. Maths is really very important for progress in CP. BTW thank you for your solution.
It can be done by combinatorics
https://www.codechef.com/viewsolution/20517309