atcoder#AGC030C. [AGC030C] Coloring Torus

[AGC030C] Coloring Torus

Score : 10001000 points

Problem Statement

For an n×nn \times n grid, let (r,c)(r, c) denote the square at the (r+1)(r+1)-th row from the top and the (c+1)(c+1)-th column from the left. A good coloring of this grid using KK colors is a coloring that satisfies the following:

  • Each square is painted in one of the KK colors.
  • Each of the KK colors is used for some squares.
  • Let us number the KK colors 1,2,...,K1, 2, ..., K. For any colors ii and jj (1iK,1jK1 \leq i \leq K, 1 \leq j \leq K), every square in Color ii has the same number of adjacent squares in Color jj. Here, the squares adjacent to square (r,c)(r, c) are $((r-1)\; mod\; n, c), ((r+1)\; mod\; n, c), (r, (c-1)\; mod\; n)$ and (r,(c+1)  mod  n)(r, (c+1)\; mod\; n) (if the same square appears multiple times among these four, the square is counted that number of times).

Given KK, choose n between 1 and 500 (inclusive) freely and construct a good coloring of an n×nn \times n grid using KK colors. It can be proved that this is always possible under the constraints of this problem,

Constraints

  • 1K10001 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

KK

Output

Output should be in the following format:

nn

c0,0c_{0,0} c0,1c_{0,1} ...... c0,n1c_{0,n-1}

c1,0c_{1,0} c1,1c_{1,1} ...... c1,n1c_{1,n-1}

::

cn1,0c_{n-1,0} cn1,1c_{n-1,1} ...... cn1,n1c_{n-1,n-1}

nn should represent the size of the grid, and 1n5001 \leq n \leq 500 must hold. cr,cc_{r,c} should be an integer such that 1cr,cK1 \leq c_{r,c} \leq K and represent the color for the square (r,c)(r, c).

2
3
1 1 1
1 1 1
2 2 2
  • Every square in Color 11 has three adjacent squares in Color 11 and one adjacent square in Color 22.
  • Every square in Color 22 has two adjacent squares in Color 11 and two adjacent squares in Color 22.

Output such as the following will be judged incorrect:

2
1 2
2 2
3
1 1 1
1 1 1
1 1 1
9
3
1 2 3
4 5 6
7 8 9