#P13054. [GCJ 2020 #1A] Pascal Walk

    ID: 12859 Type: RemoteJudge 20000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>数学2020Special Judge构造Google Code Jam

[GCJ 2020 #1A] Pascal Walk

题目描述

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)(r, k) as the kk-th position from the left in the rr-th row, with both rr and kk counted starting from 1. Then Pascal's triangle is defined by the following rules:

  • The rr-th row contains rr positions (r,1),(r,2),,(r,r)(r, 1),(r, 2), \ldots,(r, r).
  • The numbers at positions (r,1)(r, 1) and (r,r)(r, r) are 1 , for all rr.
  • The number at position (r,k)(r, k) is the sum of the numbers at positions (r1,k1)(r-1, k-1) and (r1,k)(r-1, k), for all kk with 2kr12 \leqslant k \leqslant r-1.

The first 5 rows of Pascal's triangle look like this:

In this problem, a Pascal walk is a sequence of s\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:

  • r1=1\mathrm{r}_{1}=1 and k1=1\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 ij\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 S500\mathrm{S} \leqslant 500 positions such that the sum of the numbers in all of the positions it visits is equal to N\mathrm{N}. It is guaranteed that at least one such walk exists for every N\mathrm{N}.

输入格式

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

输出格式

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

3
1
4
19
Case #1:
1 1
Case #2:
1 1
2 1
2 2
3 3
Case #3:
1 1
2 2
3 2
4 3
5 3
5 2
4 1
3 1

提示

Sample Explanation

In Sample Case #1, only the starting position is needed.

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.

The following image depicts our solution to Sample Case #3:

Limits

  • 1T1001 \leqslant \mathrm{T} \leqslant 100.

Test set 1 (3 Pts, Visible Verdict)

  • 1N5011 \leqslant \mathrm{N} \leqslant 501.

Test set 2 (11 Pts, Visible Verdict)

  • 1N10001 \leqslant \mathrm{N} \leqslant 1000.

Test set 3 (21 Pts, Hidden Verdict)

  • 1N1091 \leqslant \mathrm{N} \leqslant 10^{9}.