P13054 [GCJ 2020 #1A] Pascal Walk

Description

Pascal's triangle consists of an infinite number of rows of an increasing number of integers each, arranged in a triangular shape. Let us define $(r, k)$ as the $k$-th position from the left in the $r$-th row, with both $r$ and $k$ counted starting from 1. Then Pascal's triangle is defined by the following rules: - The $r$-th row contains $r$ positions $(r, 1),(r, 2), \ldots,(r, r)$. - The numbers at positions $(r, 1)$ and $(r, r)$ are 1 , for all $r$. - The number at position $(r, k)$ is the sum of the numbers at positions $(r-1, k-1)$ and $(r-1, k)$, for all $k$ with $2 \leqslant k \leqslant r-1$. The first 5 rows of Pascal's triangle look like this: ![](https://cdn.luogu.com.cn/upload/image_hosting/6s8m35j7.png) In this problem, a Pascal walk is a sequence of $\mathrm{s}$ positions $\left(\mathrm{r}_{1}, \mathrm{k}_{1}\right),\left(\mathrm{r}_{2}, \mathrm{k}_{2}\right), \ldots,\left(\mathrm{r}_{\mathrm{s}}, \mathrm{k}_{\mathrm{s}}\right)$ in Pascal's triangle that satisfy the following criteria: - $\mathrm{r}_{1}=1$ and $\mathrm{k}_{1}=1$. - Each subsequent position must be within the triangle and adjacent (in one of the six possible directions) to the previous position. That is, for all $\mathrm{i} \geqslant 1,\left(\mathrm{r}_{\mathrm{i}+1}, \mathrm{k}_{\mathrm{i}+1}\right)$ must be one of the following that is within the triangle: $\left(\mathrm{r}_{\mathrm{i}}-1, \mathrm{k}_{\mathrm{i}}-1\right),\left(\mathrm{r}_{\mathrm{i}}-1, \mathrm{k}_{\mathrm{i}}\right),\left(\mathrm{r}_{\mathrm{i}}, \mathrm{k}_{\mathrm{i}}-1\right),\left(\mathrm{r}_{\mathrm{i}}, \mathrm{k}_{\mathrm{i}}+1\right),\left(\mathrm{r}_{\mathrm{i}}+1, \mathrm{k}_{\mathrm{i}}\right),\left(\mathrm{r}_{\mathrm{i}}+1, \mathrm{k}_{\mathrm{i}}+1\right)$. - No position may be repeated within the sequence. That is, for every $\mathrm{i} \neq \mathrm{j}$, either $\mathrm{r}_{\mathrm{i}} \neq \mathrm{r}_{\mathrm{j}}$ or $\mathrm{k}_{\mathrm{i}} \neq \mathrm{k}_{\mathrm{j}}$, or both. Find any Pascal walk of $\mathrm{S} \leqslant 500$ positions such that the sum of the numbers in all of the positions it visits is equal to $\mathrm{N}$. It is guaranteed that at least one such walk exists for every $\mathrm{N}$.

Input Format

The first line of the input gives the number of test cases, $\mathrm{T}$. $\mathrm{T}$ test cases follow. Each consists of a single line containing a single integer $\mathrm{N}$.

Output Format

For each test case, first output a line containing case #x:, where $\mathrm{x}$ is the test case number (starting from 1). Then, output your proposed Pascal walk of length $\mathrm{S} \leqslant 500$ using $\mathrm{S}$ additional lines. The $\mathrm{i}$-th of these lines must be $\mathrm{r}_{\mathrm{i}} \mathrm{k}_{\mathrm{i}}$ where $\left(\mathrm{r}_{\mathrm{i}}, \mathrm{k}_{\mathrm{i}}\right)$ is the $\mathrm{i}$-th position in the walk. For example, the first line should be $1 \quad 1$ since the first position for all valid walks is $(1,1)$. The sum of the numbers at the $\mathrm{S}$ positions of your proposed Pascal walk must be exactly $\mathrm{N}$.

Explanation/Hint

**Sample Explanation** In Sample Case #1, only the starting position is needed. ![](https://cdn.luogu.com.cn/upload/image_hosting/06jwicgw.png) In Sample Case #2, notice that although a shorter path exists, the path does not need to be of minimal length, as long as it uses no more than 500 positions. ![](https://cdn.luogu.com.cn/upload/image_hosting/scfogipe.png) The following image depicts our solution to Sample Case #3: ![](https://cdn.luogu.com.cn/upload/image_hosting/x6b2j5as.png) **Limits** - $1 \leqslant \mathrm{T} \leqslant 100$. **Test set 1 (3 Pts, Visible Verdict)** - $1 \leqslant \mathrm{N} \leqslant 501$. **Test set 2 (11 Pts, Visible Verdict)** - $1 \leqslant \mathrm{N} \leqslant 1000$. **Test set 3 (21 Pts, Hidden Verdict)** - $1 \leqslant \mathrm{N} \leqslant 10^{9}$.