### Siriuslight's blog

By Siriuslight, history, 12 months ago, ,

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

• 0

 » 12 months ago, # |   0 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.
•  » » 12 months ago, # ^ |   0 Can you please explain your idea. I couldn't understand the editorial.
•  » » 12 months ago, # ^ |   0 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.
•  » » » 12 months ago, # ^ |   0
•  » » » » 12 months ago, # ^ |   0 Thanks :)
•  » » » » » 12 months ago, # ^ |   0 The O(K) solution has been mentioned in the editorial.
 » 12 months ago, # | ← Rev. 2 →   +1 I solved it by Lagrange InterpolationLet f(n, k) denote the answer then we have the recurrence f(n, k) - f(n - 1, k) = f(n - 1, k - 1) + f(n - 1, k - 2)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
•  » » 12 months ago, # ^ | ← Rev. 2 →   0 I have heard of this term first time. Maths is really very important for progress in CP. BTW thank you for your solution.
 » 12 months ago, # |   0 It can be done by combinatoricshttps://www.codechef.com/viewsolution/20517309
•  » » 12 months ago, # ^ |   0 Can you please explain it a bit...
 » 12 months ago, # |   0 Wrote a DP for N and K = 5K and fed the values of (N,1)....(N,K) into BM algo given in the blog HERE