Algorithm title: UVA 348 Optimal Array multiplication Sequence (interval dp)

Source: Internet
Author: User
Tags arrays final

Optimal Array Multiplication Sequence

Given two arrays A and B, we can determine the array C = A B using the standard definition of matrix multiplication:

The number of columns in the A array must is the same as the number of rows in the B array. Notationally, let's say that rows (a) andcolumns (a) are the number of rows and columns, respectively, in the A array. The number of individual multiplications required to compute the entire C array (which'll have the same number of rows a S A and the same number of columns as B) is then rows (a) columns (b) columns (a). For example, if a are a tex2html_wrap_inline67 array, and B is a tex2html_wrap_inline71 array, it would take tex2html_wrap_i Nline73, or 3000 multiplications to compute the C array.

To perform multiplication of the more than two arrays we have a choice of it to proceed. For example, if X, y, and Z are arrays, then to compute X y z we could either compute (X y) z or x (y z). Suppose X is a tex2html_wrap_inline103 array, Y are a tex2html_wrap_inline67 array, and Z is a tex2html_wrap_inline111 arra Y. Let's look at the number of multiplications required to compute the product using the two different sequences:

(X Y) Z

tex2html_wrap_inline119 multiplications to determine the product (X Y), a tex2html_wrap_inline123 array.

The final result of Then tex2html_wrap_inline125 multiplications to determine.

Total multiplications:4500.

X (Y Z)

tex2html_wrap_inline133 multiplications to determine the product (Y Z), a tex2html_wrap_inline139 array.

The final result of Then tex2html_wrap_inline141 multiplications to determine.

Total multiplications:8750.

Clearly we ' ll be able to compute (X Y) Z using fewer individual multiplications.

Given the size of each array in a sequence of arrays to is multiplied, you are to determine a optimal computational nCE. Optimality, for this problem, are relative to the number of individual multiplications required.

Input

For each array in the multiple sequences of a arrays to is multiplied you would be given only the dimensions of the array. Each sequence would consist of an integer N which indicates the number of arrays to is multiplied, and then N pairs of inte Gers, each pair giving the number of rows and columns in an array; The order in which the dimensions are given are the same as the order in which the arrays are. A value of zero for N indicates the "end of" the input. N would be no larger than 10.

Output

Assume the arrays are named tex2html_wrap_inline157. Your output for each input, the ' to be ' a line containing a parenthesized expression clearly indicating the order in whic h The arrays are to be multiplied. Prefix the output for each case with the case number (they are sequentially numbered, starting with 1). Your output should strongly resemble that shown in the samples shown below. If, by chance, there are multiple correct sequences, any of these would be accepted as a valid answer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.