Teaching Mastery
Limits: 4 sec., 256 MiB
This statement was translated automatically from Ukrainian.
While studying at Ivan Franko National University of Lviv, Zenyk was always impressed by the skill and professionalism of the professors. He often thought that he would definitely become a teacher in the future, because teaching is so incredibly interesting. Zenyk tried very hard to achieve his goal, but due to the complexity of the profession, he was not given the opportunity to teach students. Of course, Zenyk is a very determined person and does not give up his dreams, so he finally achieved the opportunity to teach at his favorite university, except that he now lays tiles (in Ukrainian, the words for "to teach" and "to lay [tiles]" share the same root).
Zenyk has to lay 2 more rows of tiles, with \(n\) cells each. Each row has a height of one cell. He has tiles of size \(1 \times 1\) and \(1 \times 2\). He must lay them row by row, but he can rotate them by 90 degrees, meaning he can rotate a \(1 \times 2\) tile to get a \(2 \times 1\) tile.
Zenyk is very interested in the following question: in how many ways can he finish laying the tiles if in the first row he has already filled the first \(a\) cells, and in the second — the first \(b\). He has \(q\) such questions. Help him answer them. Since the answer can be very large, print it modulo \(10^9 + 7\).
Input
The first line contains two integers \(n\) and \(q\) — the width of the rows for tiles and the number of queries, respectively.
The next \(q\) lines each contain two integers \(a_i\) and \(b_i\) — the number of filled cells in the first and second rows, respectively.
Output
In \(q\) lines, output one integer each — the number of ways to completely fill the two rows modulo \(10^9 + 7\).
Constraints
\(1 \le n, q \le 10^5\),
\(0 \le a_i, b_i \le n\).
Problem evaluation consists of the following subtasks:
1 point — example from the statement,
5 points — a block of tests in which \(a_i = n\),
5 points — a block of tests in which \(a_i = b_i\),
5 points — a block of tests in which \(q = 1\),
9 points — a block of tests in which \(1 \le n, q \le 10^5\) and \(0 \le a_i, b_i \le n\).
Points for a subtask will be awarded only if you give the correct answer to all tests in the subtask.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 5 2 3 3 3 4 | 7 3 |
Notes
In the case where \(a_i = b_i = n\), Zenyk does not need to place any tiles, and therefore there is one way to completely fill the two rows — placing no tiles at all.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Teaching Mastery - Editorial
Consider the first test group. There we need to fill a rectangle of size \(1 \times (n - b[i])\), so let’s learn to solve the filling problem for a \(1 \times m\) rectangle. Consider the rightmost tile in this rectangle. It can be of size 1 or of size 2. After placing one of these tiles, a rectangle of size \(1 \times (m - 1)\) or \(1 \times (m - 2)\) remains, respectively. Define \(f[m]\) as the answer for a \(1 \times m\) rectangle. Then we can write the equation \(f[m] = f[m - 1] + f[m - 2]\), with \(f[0]\) and \(f[1]\) equal to 1. This array can be computed with a simple loop.
Let’s move on to the second group. We need to fill a rectangle of size \(2 \times (n - a[i])\), that is, to solve the filling problem for a \(2 \times m\) rectangle. For this we introduce two arrays: \(g[m]\), the answer for a \(2 \times m\) rectangle, and an auxiliary \(h[m]\), the answer for the shape shown below; clearly, the answer is the same if there are more free cells on top.
Let’s write equations for them, as in the first group, by considering how the tiles are laid on the left, starting with \(h[m]\). For this shape there are two options for the tile in the leftmost free cell:
If we place a tile of size 1, we move on to the problem \(g[m - 1]\); if of size 2, to the problem \(h[m - 1]\). So \(h[m] = g[m - 1] + h[m - 1]\). Now consider the problem \(g[m]\).
Consider all possible options for the tile covering the bottom left cell:
In the first two options we move on to the problems \(h[m]\) and \(g[m - 1]\), respectively. For the third option:
Consider the options for the tile in the top left cell.
That is, we move on to the problems \(h[m - 1]\) and \(g[m - 2]\), respectively. So the final formula is \(g[m] = h[m] + g[m - 1] + h[m - 1] + g[m - 2]\). We can compute these answers in a loop, first \(h[m]\) and then \(g[m]\).
Note that \(g[1]\) equals 2, since we can take two tiles of size 1 or one of size 2. And \(g[0]\) equals 1, since we place nothing, as stated in the notes. The value \(h[1]\) equals 1, since we can place only one tile. Clearly, \(h[0] = 0\), since such a shape does not exist and cannot be covered with tiles.
Back to the original problem. Without loss of generality, let the top row need \(m\) cells and the bottom row \(d\) more.
Now consider the border between the rectangle of width 1 and the rectangle of width 2. Consider the case when a tile of size 2 stands there.
In this case we have the problem \(f[d - 1]\) on the left and \(h[m]\) on the right, and these problems are independent: for each way on the left we can choose any way on the right, so the number of options is the product of these two values. Note that this option is only possible if \(d \geq 1\) and \(m \geq 1\), so that the tile of size two has cells to stand on.
If there is no tile of length 2 on the border, we have two independent problems, one shown in blue and the other in yellow.
Then we have the problems \(f[d]\) and \(g[m]\), and, as in the previous case, we add the product of these values to the answer. So the answer is \(g[m] \cdot f[d] + h[m] \cdot f[d - 1]\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|