Showing posts with label matrix exponentiation. Show all posts
Showing posts with label matrix exponentiation. Show all posts

Wednesday, 12 September 2012

Tiles

Problem Name: Tiles
LightOJ ID: 1244
Keywords: recursion, matrix exponentiation

This is a problem involving an interesting recurrence that combines different inter–dependent relations. Moreover, given the large range of the input, it can be tackled with matrix exponentiation instead of classic dynamic programming.

You have a rectangular board of size \(2 \times N\) that is to be filled with tiles of six different types. 2 tiles are formed by two squares (each of size \(1 \times 1\)) and have the shape of a bar. The remaining 4 tiles have three squares, representing the four different rotations of a L–shaped tile. The tiles have to be placed in the board as–is (without rotating or flipping them).

We’ll identify the six tiles by the characters \(A, B, \dots, F\). This is how they look, using a single lowercase character to represent each square:

a      c   dd   e  ff
a  bb  cc  d   ee   f

The question is, in how many different ways can the board be filled using the six tiles?

It shouldn’t take long to see that it would be convenient to start by defining the following function:

\(f(n)\)
Number of ways to completely fill a board of size \(2 \times n\).

However, there is not a straightforward solution from just this relation —at least not one that I can see. Something that helps here is looking at a couple of additional “states” that can be analysed and used to build our final answer.

Consider the following auxiliary functions:

\(g(n)\)
Number of ways to fill a board of size \(2 \times (n-1)\), leaving a single square in the top row of the \(n\)th column.

and finally,

\(h(n)\)
Number of ways to fill a board of size \(2 \times (n-1)\), leaving a single square in the bottom row of the \(n\)th column.

Think for example of what happens when you have completely filled a section of the board of size \(2 \times k\), going from left to right, and you append a \(C\) tile. Then you have filled an area of \(2 \times (k+1)\), but you have an additional single square at the bottom of column \(k+2\). Those are the situations where \(g\) and \(h\) will prove useful.

Also, having these three relations make it possible to now formulate a definition of \(f(n)\):

These expressions come directly from thinking about the different ways to place the six tiles inside the board. Let’s take \(f(n)\) for example. By definition, \(f\) concerns itself only with filling the first \(n\) columns entirely, so it shouldn’t include cases where the tile \(C\) and \(D\) are placed last. It only considers the cases where the last tile is either: \(A\), represented by the term \(f(n-1)\); or \(B\), which leads to \(f(n-2)\) (using two \(B\) tiles one on top of the other); or \(E\) and \(F\), which lead to \(g(n-1)\) and \(h(n-1)\) respectively.

The definitions of \(g(n)\) and \(h(n)\) come from a similar reasoning. Try to deduce them by yourself thinking about the different ways in which you can leave one single square in the last column. The beauty of this is that now all the terms show recurrence and lend themselves to a matrix exponentiation strategy quite nicely.

Let’s outline two of the three matrices, leaving the matrix with the coefficients for last:

Now here’s the crucial step for our implementation. Given our previous definitions of \(f\), \(g\) and \(h\), we fill the matrix \(A\):

With this now we can write the implementation, and obtain the answer in \(O(k^3 log N)\), with \(k=4\).

Saturday, 19 May 2012

Summing Up Powers

Problem name: Summing Up Powers
LightOJ ID: 1132
Keywords: matrix exponentiation

This is a Matrix Exponentiation problem. With M.E., as is the case with many other concepts (like D.P.), I think it’s one thing to understand the fundamental idea, which is relatively easy and takes little time, and then another thing is developing your skills to apply the idea in different situations, which is a never-ending process.

For the first part, there is some good material on the web, like this article by Zobayer :). For the second part, it’s mostly a matter of practising. I’ll try explaining how I modelled this problem, but there are probably other ways.

Basically, the usual way to recognise if a problem can be solved by M.E. is if it involves a recurrence relation, and its constraints are too large for a D.P. solution. More specifically, if we can express the recurrence relation as a function \(f\), that depends on a set of functions \(g_1, g_2, \dots, g_k\), and a set of coefficients \(a_1, a_2, \dots, a_k\):

If all the \(g_i\) functions are also recurrence relations which can be deduced from known elements, then you’re all set! You just need to fill three matrices \(A, B, C\), such that \(A \times B = C\). The \(B\) matrix has size \(k \times 1\), and is filled the expressions \(g_1(x), g_2(x), \dots, g_k(x)\). The \(C\) matrix is also \(k \times 1\), and is filled with \(g_1(x+1), g_2(x+1), \dots, g_k(x+1)\), and the matrix \(A\) (size \(k \times k\)) is filled with coefficients that are easy to determine using \(a_1, a_2, \dots, a_k\), and the fact that the expression \(A \times B = C\) needs to evaluate to true. By the way, I’m not a mathematician and I could be abusing the language around here, but I hope the main idea is clear :).

Now, let’s try to apply all this to this problem. A first attempt to express the function as a recurrence relation can be like this:

Now, from the right hand of [1], we can see that \(f(x)\) is a recurrence relation, and \(f(x+1)\) is already defined from known expressions. What is not so apparent, is the second expression. How to calculate \((x+2)^k\) from the expressions we have?

We’ll, let’s start by expanding the expression. Thanks to Pascal’s triangle, we know that expanding the binomial \((x+1)^k\) produces the following:

This looks better. Now we have a set of binomial coefficients, and a set of expressions of the form \(x^i\), which have recurrence, meaning that we could calculate \((x+1)^i\) using the expressions we have already.

Now let’s put this in matrix form. Let’s begin with \(B\) and \(C\):

And knowing what we know, fill the matrix of coefficients:

There you go. To complete the solution, all the binomial coefficients could be pre–calculated, and then simple matrix operations would produce the result in \(O(k^3 \log{x})\). Really impressive!