My matrix multiplication travelogue

Given K, construct matrix dimensions whose worst-case chain multiplication cost minus optimal cost equals exactly K.

Medium6Dynamic programmingMathGreedyImplementationNo attempts yetTime limit1sMemory limit128 MB

Problem

Computing the product of nn matrices M1,M2,,Mn\mathbf{M}_1, \mathbf{M}_2, \cdots, \mathbf{M}_n, that is M1M2Mn\mathbf{M}_1 \mathbf{M}_2 \cdots \mathbf{M}_n, is tedious for a person and for a computer alike.

Here is a summary for anyone who is not used to matrices and their products. A matrix is a rectangular arrangement of numbers or symbols wrapped in parentheses. In this problem every entry of a matrix is an integer. For example, the following is a matrix.

(201605281417)\begin{pmatrix} 2 & 0 & 1 & 6 \\ 0 & 5 & 2 & 8 \\ 1 & 4 & -1 & 7 \end{pmatrix}

The numbers arranged in a matrix are called entries. A horizontal line of a matrix is called a row, and the rows are named row 1, row 2, row 3, ... from the top. A vertical line of a matrix is called a column, and the columns are named column 1, column 2, column 3, ... from the left. A matrix with mm rows and nn columns is an m×nm \times n matrix. The entry in row ii and column jj is the (i,j)(i, j) entry of the matrix, and the (i,j)(i, j) entry of a matrix A\mathbf{A} is written AijA_{ij}.

Just like multiplication of real numbers, matrix multiplication takes two matrices. When A\mathbf{A} is an m×nm \times n matrix and B\mathbf{B} is an n×pn \times p matrix, the product AB\mathbf{AB} is defined as the m×pm \times p matrix whose (i,j)(i, j) entry is

(AB)ij=k=1nAikBkj(\mathbf{AB})_{ij} = \sum_{k=1}^{n} A_{ik} B_{kj}

Computing one entry of AB\mathbf{AB} takes nn integer multiplications, and AB\mathbf{AB} is an m×pm \times p matrix, so computing every entry takes (m×p)×n=m×n×p(m \times p) \times n = m \times n \times p integer multiplications in total.

The product is defined only when the number of columns of A\mathbf{A} equals the number of rows of B\mathbf{B}. For example, a 3×23 \times 2 matrix and a 4×54 \times 5 matrix cannot be multiplied.

Matrix multiplication is not commutative, but it is associative. That is, for an m×nm \times n matrix A\mathbf{A}, an n×pn \times p matrix B\mathbf{B}, and a p×qp \times q matrix C\mathbf{C}, the following holds in general.

  • ABBA\mathbf{AB} \neq \mathbf{BA}
  • ABC=(AB)C=A(BC)\mathbf{ABC} = (\mathbf{AB})\mathbf{C} = \mathbf{A}(\mathbf{BC})

When several matrices are multiplied, the order in which they are listed cannot change, but the order in which the products are carried out is free. Does changing that order really change the number of integer multiplications? Let A\mathbf{A} be a 2×42 \times 4 matrix, B\mathbf{B} a 4×34 \times 3 matrix, and C\mathbf{C} a 3×53 \times 5 matrix, and compute the product ABC\mathbf{ABC}.

  • Computing it as (AB)C(\mathbf{A}\mathbf{B})\mathbf{C}
    • Multiplying the 2×42 \times 4 matrix A\mathbf{A} by the 4×34 \times 3 matrix B\mathbf{B} takes 2×4×3=242 \times 4 \times 3 = 24 integer multiplications and produces a 2×32 \times 3 matrix.
    • Multiplying the 2×32 \times 3 matrix AB\mathbf{A}\mathbf{B} by the 3×53 \times 5 matrix C\mathbf{C} takes 2×3×5=302 \times 3 \times 5 = 30 integer multiplications and produces a 2×52 \times 5 matrix.
    • The total is 24+30=5424 + 30 = 54 integer multiplications.
  • Computing it as A(BC)\mathbf{A}(\mathbf{B}\mathbf{C})
    • Multiplying the 4×34 \times 3 matrix B\mathbf{B} by the 3×53 \times 5 matrix C\mathbf{C} takes 4×3×5=604 \times 3 \times 5 = 60 integer multiplications and produces a 4×54 \times 5 matrix.
    • Multiplying the 2×42 \times 4 matrix A\mathbf{A} by the 4×54 \times 5 matrix BC\mathbf{B}\mathbf{C} takes 2×4×5=402 \times 4 \times 5 = 40 integer multiplications and produces a 2×52 \times 5 matrix.
    • The total is 60+40=10060 + 40 = 100 integer multiplications.

The count already depends on the order for three matrices, so it depends on the order for nn matrices as well. The more matrices there are, the more ways there are to multiply them. For example, when n=4n = 4 there are five ways to multiply M1,M2,M3,M4\mathbf{M}_1, \mathbf{M}_2, \mathbf{M}_3, \mathbf{M}_4.

  • ((M1M2)M3)M4\big((\mathbf{M}_1\mathbf{M}_2)\mathbf{M}_3\big)\mathbf{M}_4
  • (M1(M2M3))M4\big(\mathbf{M}_1(\mathbf{M}_2\mathbf{M}_3)\big)\mathbf{M}_4
  • (M1M2)(M3M4)(\mathbf{M}_1\mathbf{M}_2)(\mathbf{M}_3\mathbf{M}_4)
  • M1((M2M3)M4)\mathbf{M}_1\big((\mathbf{M}_2\mathbf{M}_3)\mathbf{M}_4\big)
  • M1(M2(M3M4))\mathbf{M}_1\big(\mathbf{M}_2(\mathbf{M}_3\mathbf{M}_4)\big)

Every way gives the same result, so picking the way that needs the fewest integer multiplications minimizes the time spent multiplying.

Seunghyun likes computing, and he recently learned how to multiply nn matrices like this. After working through a few examples he fell for multiplication. He says that multiplying entries and adding all of the products up is beautiful.

Seunghyun performs an integer multiplication in zero time, so he began to wonder whether the number of integer multiplications really has to be minimized, and he asked his teacher. The teacher answered that the gap between the worst count and the optimal count is sometimes quite large, so for an ordinary person, who spends real time on an integer multiplication, keeping the count small matters. Here the worst and the optimal count of integer multiplications are the counts of the way that needs the most and the way that needs the fewest integer multiplications, among all the ways of multiplying the matrices.

Seunghyun could not relate to ordinary people at all, so he kept asking. Worn out by the questions, the teacher finally answered that for every natural number KK there are matrices M1,M2,,Mn\mathbf{M}_1, \mathbf{M}_2, \cdots, \mathbf{M}_n whose worst count and optimal count of integer multiplications differ by exactly KK.

Shocked, Seunghyun asked you to find one such family of matrices. Given KK, write a program that finds the sizes of matrices whose worst count and optimal count of integer multiplications differ by exactly KK.

Input

The first line contains an integer KK (1K1091 \le K \le 10^9).

Output

On the first line print the number of matrices NN (1N1001 \le N \le 100). On the second line print N+1N+1 positive integers a0,a1,,aNa_0, a_1, \ldots, a_N, separated by spaces, giving the sizes of the matrices. The matrix sizes are a0×a1a_0 \times a_1, a1×a2a_1 \times a_2, ..., aN1×aNa_{N-1} \times a_N in that order, and the worst count and the optimal count of integer multiplications for multiplying these NN matrices must differ by exactly KK.

If several answers satisfy the condition, print only the lexicographically smallest one. That is, first take an answer with the smallest NN, and among answers with the same NN take the one whose sequence (a0,a1,,aN)(a_0, a_1, \ldots, a_N) is smallest when compared from the front.