Polly Nomials

Time limit1sMemory limit128 MB

Summary
For each polynomial with leading coefficient 1, evaluate it at x = 1 or -1 and find the minimum key presses needed to compute it on a left-to-right calculator.
Level

Medium7 of 10

Topics
Dynamic programming, Math, Greedy, Implementation
Solved
No attempts yet

Problem

The International Ornithologists Union runs the Avian Computation Mission (ACM), which studies the computational ability of birds. In its most famous project, the "Polly Nomial" project, parrots are trained to evaluate simple polynomials in one variable xx with non-negative integer coefficients.

Each parrot uses a Parrot Digital Assistant (PDA), a beak-operated calculator. Its keys are the digits 00 through 99, the symbol xx, and the operators ++, ×\times, and ==. The xx key stands for the variable; its numeric value is set internally for testing, but the parrot only ever sees the symbol xx.

The PDA works like a basic immediate-execution calculator: it has NO extra memory and NO operator precedence. It evaluates strictly left to right, applying each ++ or ×\times immediately to the value currently on the display and the next operand that is entered. An operand is either the variable xx or a non-negative integer typed one digit at a time.

For example, to compute x3+x+11x^3 + x + 11 a parrot might press:

x, ×, x, ×, x, +, x, +, 1, 1, =x,\ \times,\ x,\ \times,\ x,\ +,\ x,\ +,\ 1,\ 1,\ =

which evaluates as ((((x×x)×x)+x)+11)((((x \times x) \times x) + x) + 11).

Because the PDA has no memory, the parrot cannot store a partial result. For a polynomial such as x3+2x2+11x^3 + 2x^2 + 11 it cannot first compute x3x^3 and set it aside while computing 2x22x^2; it must instead choose an order of operations (for instance a nested, Horner-style scheme) that reaches the answer using only the single display.

The cost of a computation is the total number of key presses, including the final ==. For x3+x+11x^3 + x + 11 the sequence above costs 1111: four presses of xx, two of ×\times, two of ++, two presses of the digit 11, and one ==. This happens to be the minimum possible cost for that polynomial.

Write a program that, for each given polynomial, reports its value at the given xx and the minimum possible key-press cost. Because parrots are intimidated by a leading coefficient other than 11, the highest-degree coefficient is always 11.

Input

Each line describes one polynomial anxn+an−1xn−1+⋯+a1x+a0a_n x^n + a_{n-1} x^{n-1} + \cdots + a_1 x + a_0. A line begins with the degree nn (1≤n≤1001 \le n \le 100), followed by the n+1n+1 non-negative coefficients an,an−1,…,a0a_n, a_{n-1}, \ldots, a_0 in order of decreasing power (with an=1a_n = 1 always), and finally the integer value of xx, which is always either 11 or −1-1. The input ends with a line containing the two values 0 0, which must not be processed.

Output

For each polynomial, print a line of the form Polynomial i: value cost, where i is the polynomial's position in the input (starting from 11), value is the polynomial evaluated at the given xx, and cost is the minimum number of key presses (including the final ==) needed to compute it.

Examples4

  1. Example 1

    Input
    3 1 0 1 11 1
    3 1 0 2 11 -1
    0 0
    
    Expected output
    Polynomial 1: 13 11
    Polynomial 2: 8 11
    
  2. Example 2

    Input
    1 1 5 1
    0 0
    
    Expected output
    Polynomial 1: 6 4
    
  3. Example 3

    Input
    1 1 5 -1
    0 0
    
    Expected output
    Polynomial 1: 4 4
    
  4. Example 4

    Input
    2 1 0 7 1
    3 1 2 3 4 -1
    1 1 9 1
    0 0
    
    Expected output
    Polynomial 1: 8 6
    Polynomial 2: 2 12
    Polynomial 3: 10 4