Given K, construct matrix dimensions whose worst-case chain multiplication cost minus optimal cost equals exactly K.
Medium6Dynamic programmingMathGreedyImplementationNo attempts yetTime limit1sMemory limit128 MBComputing the product of n matrices M1,M2,⋯,Mn, that is M1M2⋯Mn, 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.
20105412−1687
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 m rows and n columns is an m×n matrix. The entry in row i and column j is the (i,j) entry of the matrix, and the (i,j) entry of a matrix A is written Aij.
Just like multiplication of real numbers, matrix multiplication takes two matrices. When A is an m×n matrix and B is an n×p matrix, the product AB is defined as the m×p matrix whose (i,j) entry is
(AB)ij=∑k=1nAikBkj
Computing one entry of AB takes n integer multiplications, and AB is an m×p matrix, so computing every entry takes (m×p)×n=m×n×p integer multiplications in total.
The product is defined only when the number of columns of A equals the number of rows of B. For example, a 3×2 matrix and a 4×5 matrix cannot be multiplied.
Matrix multiplication is not commutative, but it is associative. That is, for an m×n matrix A, an n×p matrix B, and a p×q matrix C, 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 A be a 2×4 matrix, B a 4×3 matrix, and C a 3×5 matrix, and compute the product ABC.
The count already depends on the order for three matrices, so it depends on the order for n matrices as well. The more matrices there are, the more ways there are to multiply them. For example, when n=4 there are five ways to multiply M1,M2,M3,M4.
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 n 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 K there are matrices M1,M2,⋯,Mn whose worst count and optimal count of integer multiplications differ by exactly K.
Shocked, Seunghyun asked you to find one such family of matrices. Given K, write a program that finds the sizes of matrices whose worst count and optimal count of integer multiplications differ by exactly K.
The first line contains an integer K (1≤K≤109).
On the first line print the number of matrices N (1≤N≤100). On the second line print N+1 positive integers a0,a1,…,aN, separated by spaces, giving the sizes of the matrices. The matrix sizes are a0×a1, a1×a2, ..., aN−1×aN in that order, and the worst count and the optimal count of integer multiplications for multiplying these N matrices must differ by exactly K.
If several answers satisfy the condition, print only the lexicographically smallest one. That is, first take an answer with the smallest N, and among answers with the same N take the one whose sequence (a0,a1,…,aN) is smallest when compared from the front.