Pro.ID22554 TitlePolly Nomials Title链接http://10.20.2.8/oj/exercise/problem?problem_id=22554 AC0 Submit0 Ratio- 时间&空间限制描述The Avian Computation Mission of the International Ornithologists Union is dedicated to the study of intelligence in birds, and specifically the study of computational ability. One of the most promising projects so far is the "Polly Nomial" project on parrot intelligence, run by Dr. Albert B. Tross and his assistants, Clifford Swallow and Perry Keet. In the ACM, parrots are trained to carry out simple polynomial computations involving integers, variables, and simple arithmetic operators. x3 + x + 11 the parrot might tap the following sequence of symbols: x , x , x , x , x , + , x , + , 1 , 1 , = The PDA has no extra memory, so each x or + operation is applied to the previous contents of the display and whatever succeeding operand is entered. If the polynomial had been x3 + 2x2 + 11 then the parrot would not have been able to "save" the value of x3 while calculating the value of 2x2. Instead, a different order of operations would be needed, for instance: x , + , 2 , x , x , x , x , + , 1 , 1 , = The cost of a calculation is the number of key presses. The cost of computing x3+x+11 in the example above is 11 (four presses of the x key, two presses of 'x', two presses of '+', two presses of the digit '1', and the '=' key). It so happens that this is the minimal cost for this particular expression using the PDA. 输入Input consists of a sequence of lines, each containing a polynomial and an x value. Each polynomial anxn + an-1xn-1 + ... + a0 is represented by its degree followed by the non-negative coefficients an ... a0 of decreasing powers of x, where an is always 1. Degrees are between 1 and 100. The coefficients are followed on the same line by an integer value for the variable x, which is always either 1 or -1. The input is terminated by a single line containing the values 0 0. 输出Description The Avian Computation Mission of the International Ornithologists Union is dedicated to the study of intelligence in birds, and specifically the study of computational ability. One of the most promising projects so far is the "Polly Nomial" project on parrot intelligence, run by Dr. Albert B. Tross and his assistants, Clifford Swallow and Perry Keet. In the ACM, parrots are trained to carry out simple polynomial computations involving integers, variables, and simple arithmetic operators. x3 + x + 11 the parrot might tap the following sequence of symbols: x , x , x , x , x , + , x , + , 1 , 1 , = The PDA has no extra memory, so each x or + operation is applied to the previous contents of the display and whatever succeeding operand is entered. If the polynomial had been x3 + 2x2 + 11 then the parrot would not have been able to "save" the value of x3 while calculating the value of 2x2. Instead, a different order of operations would be needed, for instance: x , + , 2 , x , x , x , x , + , 1 , 1 , = The cost of a calculation is the number of key presses. The cost of computing x3+x+11 in the example above is 11 (four presses of the x key, two presses of 'x', two presses of '+', two presses of the digit '1', and the '=' key). It so happens that this is the minimal cost for this particular expression using the PDA. Input Input consists of a sequence of lines, each containing a polynomial and an x value. Each polynomial anxn + an-1xn-1 + ... + a0 is represented by its degree followed by the non-negative coefficients an ... a0 of decreasing powers of x, where an is always 1. Degrees are between 1 and 100. The coefficients are followed on the same line by an integer value for the variable x, which is always either 1 or -1. The input is terminated by a single line containing the values 0 0. Output For each polynomial, print the polynomial number followed by the value of the polynomial at the given integer value x and the minimum cost of computing the polynomial; imitate the formatting in the sample output. Sample Input 3 1 0 1 11 1 3 1 0 2 11 -1 0 0 Sample Output Polynomial 1: 13 11 Polynomial 2: 8 11 Source 样例输入3 1 0 1 11 1 3 1 0 2 11 -1 0 0 样例输出Polynomial 1: 13 11 Polynomial 2: 8 11 作者 |