Hi Everyone ! I am not able to understand how to break this problem into subproblems ?
Can someone help please ? Here is the problem link : Problem
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | adamant | 152 |
| 3 | Proof_by_QED | 146 |
| 3 | Um_nik | 146 |
| 5 | Dominater069 | 144 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 9 | TheScrasse | 134 |
| Name |
|---|



Just Bumping it . In case someone wanna help .
Think about the last row of the tower. You can either have both cells as a part of the same block, or both cells as parts of different blocks. Let's call towers of the first kind type-1 towers, and the towers of the second kind type-2 towers.
Let's call $$$a_n$$$ be the number of type-1 towers and $$$b_n$$$ be the number of type-2 towers.
For computing $$$a_n$$$, look at what remains when you remove the last row from the tower. Corresponding to a type-1 tower of height $$$n - 1$$$, there will be two such towers, and corresponding to a type-2 tower of height $$$n - 1$$$, there will be one such tower, so $$$a_n = 2a_{n - 1} + b_{n - 1}$$$. In a similar manner you have $$$b_n = 4b_{n - 1} + a_{n - 1}$$$.
Then you can either precompute the answers to all possible queries, or use matrix exponentiation to solve the recurrence. The answer is $$$a_n + b_n$$$.
Thanks a lot .
When computing bn, you consider the last row to build the type-2 tower right ? But I don't understand why it can be 4 * b(n-1) + a(n — 1) sir. Thanks you if you can explain to me!!
Because the lower n-1 level can have either one a[n-1] and one b[n-1] because of solid border, or 2 b[n-1] breaking any of the borders, and b[n-1] when breaking both the borders. Here border is the line between the nth and (n-1)th row.
I don't understand the explanation of the type-1 and type-2 block, can someone give me and example? thanks
can someone tell me the rating of this problem acc to codeforces
1800
I came up with another solution which I think is easier to understand (obvs im biased)
Call a "floor" a tower that can't be split horizontally. A floor of size n can be a 2xn block (1 way) or two 1xn blocks next to eachother such that the two blocks are never broken on the same layer. The second type of floor has n-1 layers. Each can be broken to the left, the right, or not broken at all ($$$3^{n-1}$$$) ways, which means the number of floors of size n is $$$f_n = 1 + 3^{n-1}$$$
All towers of size n are made of a tower size j, with a floor of size n-j on top. Thus, the number of towers size n is $$$\displaystyle t_n = \sum_{j=0}^{n-1} t_j f_{n-j}$$$. This takes $$$O(n^2)$$$ time to calculate, so we simplify:
where $$$p_n$$$ is a prefix sum
This was my code:
We're building the tower level by level (row by row, from bottom to top). At each level of height 1, we have two types of configurations:
LINKED (linked = 1): One horizontal 2×1 block spanning both columns
UNLINKED (linked = 0): Two separate 1×1 blocks side by side
The DP State
dp[levels][linked] = "Number of ways to fill the remaining levels levels, starting with the given configuration (linked or unlinked)"The Transitions When current level is LINKED:
If we have a horizontal block at this level, the next level can be:
2 different linked configurations (coefficient: 2)
1 unlinked configuration (coefficient: 1)
Formula:
dp[levels][1] = 2 × dp[levels-1][1] + 1 × dp[levels-1][0]When current level is UNLINKED:
If we have two separate blocks at this level, the next level can be:
4 different unlinked configurations (coefficient: 4)
1 linked configuration (coefficient: 1)
Formula:
dp[levels][0] = 4 × dp[levels-1][0] + 1 × dp[levels-1][1]Why These Numbers (2 and 4)?
The coefficients represent different ways blocks can be placed on top of the previous configuration while considering:
How blocks align with what's below Different heights and positions of vertical blocks that result in distinct final configurations
Base Case
dp[0][0] = dp[0][1] = 1When 0 levels remain, there's exactly 1 way (we're done).Precompute: Calculate dp table for all possible n values (0 to 10^6)
Answer queries: For height n, the answer is
dp[n-1][0] + dp[n-1][1]Credit: https://www.youtube.com/watch?v=pMEYMYTX-r0
...