0
$\begingroup$

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:

Examples 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.

$\endgroup$
1
  • $\begingroup$ Your recursive definition is bit off. $2*Fun(h-1, w)$ works only when $w==2$. $\endgroup$
    – codeR
    Commented Apr 24 at 9:09

1 Answer 1

0
$\begingroup$

There are several problems in your recurrence:

  • for $h = 0$ or $w = 0$, there should be one possibility, not zero: the only way to build a tower of height or width zero is adding no block at all;

  • you write:

    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.

    but if you are considering block size 1×2, that means that you are implicitly considering $w = 2$, which is not necessarily the case;

  • you are not considering the possibility to add a (for example) $2\times 2$ block at the top of the tower, in the case $w = 2$.

What I would suggest:

  • write a function to compute the number of possibilities for a tower of width $1$ and height $H$, independently (this is easier to write and can serve as a preliminary exercise). Let us denote this function $f_1(H)$;
  • to build a tower of height $H$ and width $2$, for $H> 0$, you have to consider two cases:
    • if there are no block of width $2$, there are $f_1(H)^2$ possibilities to construct the tower (two towers of width $1$ and height $H$);
    • if the top block of width $2$ that appears in the tower starts at height $h\geqslant 0$ and has height $k$, we have to complete with two towers of width $1$ and height $H - h - k$, over one block $k\times 2$ over a tower of height $h$ and width $2$. That means: $$f_2(H) = \sum\limits_{h=0}^{H-1}\sum\limits_{k=1}^{H-h}f_2(h) \times f_1(H-h-k)^2$$
$\endgroup$
4
  • $\begingroup$ Hi thanks for the answer, you write that I am implicitly considering the width to be 2 in my recurrence: F(h,w)= 2 * F(h - 1, w). In this case what I am trying to do is find the number of towers of height h -1, with a fixed width w. Since I am first finding out the number of towers of height h -1, all I have to do is cover the last height, i.e del(h) = 1. Can you elaborate a bit further on why this is wrong? I feel that intuitively in this case the number of ways to construct the last height is two because we place two 1x1 blocks and one 1x2 block. Can you elaborate why this is wrong? $\endgroup$
    – Ved K
    Commented Apr 24 at 13:53
  • $\begingroup$ @VedK what if the last block is a $2\times 2$ block? How can your case take this tower into account, since you are only adding blocks of height $1$ at the top of the tower? $\endgroup$
    – Nathaniel
    Commented Apr 24 at 14:06
  • $\begingroup$ Thanks, it makes sense now, I guess my mistake is that I am not considering cases where it is possible that the last block or the top block can be a whole block with dimensions K X 2 where k ranges from 1 to H. Thanks I am clear now. Also how were you able to come up with an elegant recurrence so easily, anything I can do to improve my problem solving here? $\endgroup$
    – Ved K
    Commented Apr 24 at 14:12
  • $\begingroup$ There is no secret: I did not find this formula on my first try, there is a lot of trial and error, and of getting used to dynamic programming recurrence formulas. $\endgroup$
    – Nathaniel
    Commented Apr 24 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.