Sometimes there are two different recursive function for even and odd n. Such as if n is even, then f(n) = f(n-1)/2, otherwise (if n is odd) f(n) = 3f(n-1) + 1.
I know basic of matrix exponentiation on many variation but now i am facing problem on it.
please someone explain.
Not sure if there is a general way to handle such recurrences but for the current case, this should help: https://en.wikipedia.org/wiki/Collatz_conjecture
You can make both transitions at the same time. Consider n even, using your example we have f(n) = f( 3*f(n-2)+1 )/2. As n will always be even just need to apply this n/2 times. If n is odd just do one transition by hand and use the same method.
So use a new function, change the exponent properly and handle some corner cases.
hx0 has written a Python decorator (description in Russian and English) which does exactly that. You can either read his description or my example here (or, actually, both):
General way:
First, write a program which solves the problem under the following limitations:
For example, here is the one for Fibonacci numbers:
And here is another for sum of squares (12 + 22 + 32 = 1 + 4 + 9 = 14). I'll go with this example from now on:
Now, wonderful fact: the transformation you perform to that fixed set of variables on each iteration is actually a linear transformation which can be represented using matrix. You can see it that way: value of each variable after the iteration is some linear combination of values of variables before the iteration. In order to add constants, let's introduce For example, in the latter example we have the following transformation going on during a single iteration:
Note that order of equivalences is the same as order of variables in each row — it's important, because now we're going to rewrite that transformation as a matrix:
Or, shortly:
So, we've just transformed a simple loop iteration into a matrix. So, loop became matrix exponentiation and we can apply fast exponentiation trick.
UPD: I forgot to actually answer the original question. If you have different equations for odd and even values of
n
, you can pretend that you have two mutally recursive functions, e.g. instead of:We would have:
And one can calculate these two recurrences simultaneously using the algorithm from above.
Can two equation be relate all the time ?? I wanted a general solution .
Is there any general solution???
If the question is like f(n)=2*f(n-1)+6*f(n-5) if odd. f(n)=f(n-4)+f(n-5) if even. See now i have to think a lot to relate this two equation. So I want a general solution. If there is no general solution then is it possible to relate two equation all the time ?? If sometimes it is not possible then what should be done that time ??
General solution: if your formulas change every K steps, then you have to do K steps at once. If you have N functions which look at M steps behind to calculate, you'd have to use something like N × (M + K) variables to write the program I mentioned in my first post: just store last M + K values of each function and then calculate next K steps instead of only one step.
http://fusharblog.com/solving-linear-recurrence-for-programming-contest-part-2/
I have a few doubts:
1. Does anyone know any question that uses the odd-even recurrence trick, as mentioned in the blog?
2. Is the Running time same as O(k3lg(n))?
3. Also is it possible to generalise this idea for solving a piecewise defined recurrence?
In my proposed general algorithm running time is still , if we say that k is the size of the matrix and n is number of iterations.
What do you mean by "piecewise defined recurrence"? Can you give an example?