Reputation: 955
I am trying to find the recurrence relation for this problem on Codechef:
http://www.codechef.com/problems/BWALL
I know once I find it, I can easily solve it using matrix exponentiation. But I'm having trouble understanding how it gets the right answer. There is a solution here, but I'd like if someone explained it in a better manner?
Is there is a simple rule of thumb to find recurrences or something like that? Thanks!
Upvotes: 1
Views: 550
Reputation: 1878
The "general rule" to find a recurrence is to understand how is the solution of a problem related to the solutions of smaller problems. But more than that, I don't think that there's a general procedure to find the recurrence.
For this particular example, here is how you can find the recurrence.
Suppose that you have a big wall of size N. Now, just look at the end of the wall. More precisely, from the end of the wall, find the first place with a "vertical separation", i.e. the first place where you can split the wall into two smaller walls without L-shape.
Example:
(A) Here is the wall:
X###X#XXX#X
XX#XX#XXXXX
The splitting with the end gives you:
X###X#XXX #X
XX#XX#XXX XX
(B) Another wall
X###X#XXX
XX#XX#XXX
The splitting with the end gives you:
X###X# XXX
XX#XX# XXX
What is the size of the small piece of wall that you can get between the splitting and the end of the wall? Well, you can have 1, 2 or 3, but not more (otherwise, you could make a smallest splitting).
The possibilities for the small piece are actually the ones given in your question (yes, the 7 small blocks).
So, to build a wall with size N, you must:
So, the number T(N) of possible walls with size N is
And there you get your recurrence.
Hope it helps!
Upvotes: 1