My matrix multiplication travelogue
Time limit1sMemory limit128 MB
Given K, output dimension array a0..aN whose worst and best matrix-chain multiplication counts differ by exactly K, with the smallest lexicographic answer.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Matrix, Math, Combinatorics
- Solved
- No attempts yet
Problem
Computing the product of matrices , that is , 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.
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 rows and columns is an matrix. The entry in row and column is the entry of the matrix, and the entry of a matrix is written .
Just like multiplication of real numbers, matrix multiplication takes two matrices. When is an matrix and is an matrix, the product is defined as the matrix whose entry is
Computing one entry of takes integer multiplications, and is an matrix, so computing every entry takes integer multiplications in total.
The product is defined only when the number of columns of equals the number of rows of . For example, a matrix and a matrix cannot be multiplied.
Matrix multiplication is not commutative, but it is associative. That is, for an matrix , an matrix , and a matrix , the following holds in general.
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 be a matrix, a matrix, and a matrix, and compute the product .
- Computing it as
- Multiplying the matrix by the matrix takes integer multiplications and produces a matrix.
- Multiplying the matrix by the matrix takes integer multiplications and produces a matrix.
- The total is integer multiplications.
- Computing it as
- Multiplying the matrix by the matrix takes integer multiplications and produces a matrix.
- Multiplying the matrix by the matrix takes integer multiplications and produces a matrix.
- The total is integer multiplications.
The count already depends on the order for three matrices, so it depends on the order for matrices as well. The more matrices there are, the more ways there are to multiply them. For example, when there are five ways to multiply .
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 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 there are matrices whose worst count and optimal count of integer multiplications differ by exactly .
Shocked, Seunghyun asked you to find one such family of matrices. Given , write a program that finds the sizes of matrices whose worst count and optimal count of integer multiplications differ by exactly .
Input
The first line contains an integer ().
Output
On the first line print the number of matrices (). On the second line print positive integers , separated by spaces, giving the sizes of the matrices. The matrix sizes are , , ..., in that order, and the worst count and the optimal count of integer multiplications for multiplying these matrices must differ by exactly .
If several answers satisfy the condition, print only the lexicographically smallest one. That is, first take an answer with the smallest , and among answers with the same take the one whose sequence is smallest when compared from the front.