Problem Statement:
Your task is to build a tower whose width is 2 and height is n. You have an unlimited supply of blocks whose width and height are integers.
For example, here are some possible solutions for n = 6:
Given n, how many different towers can you build? Mirrored and rotated towers are counted separately if they look different. Input The first input line contains an integer t: the number of tests. After this, there are t lines, and each line contains an integer n: the height of the tower.
Output:
For each test, print the number of towers modulo 10^9+7.
Example
Input:
3
2
6
1337
Output:
8
2864
640403945
My Approach:
Consider a function Fun(h, w), where h and w stand for height and width respectively, I assume that this function recursively works and returns me the number of ways to count towers for the respective parameters.
The two subproblems I identified:
- Find the number of towers of height 'h - 1' and of the same width. That function is Fun(h - 1, w). The last height has the block size of 1x2, this can be filled in two ways, two 1x1 blocks and one 1x2 block. So by fundamental prinicpal of multiplication the number of such towers is 2*Fun(h-1, w) by fixing the width.
- Now we keep our height constant and modify our width, the recurrence I obtained here was Fun(h, w-1) * Fun(h, 1). Basically find the number of ways of constructing a tower of width 1 and height h and number of ways of constructing a tower of height h and width w - 1. On applying fundamental principle of multiplication we get the number of ways as Fun(h, w-1) * Fun(h, 1).
Finally my recurrence looks like this:
Fun(h, w) = 2 * Fun(h - 1, w) + Fun(h, w - 1) * Fun(h, 1)
if h == 0 or w == 0 return 0;
if h == 1 and w == 2 return 2;
if h == 2 and w == 1 return 1; // trivial base cases
I wanted to know whether the recurrence I have designed is valid or not, this seems to work well for h = 2 and w = 2 but fails for the rest, what am I missing here? Please try to explain.