SOJ 1040

1040. Polly Nomials

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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.

When shown a formula consisting of a polynomial with non-negative integer coefficients and one variable x, each parrot uses a special beak-operated PDA, or "Parrot Digital Assistant," to tap out a sequence of operations for computing the polynomial. The PDA operates much like a calculator. It has keys marked with the following symbols: the digits from 0 through 9, the symbol 'x', and the operators '+', '*', and '='. (The x key is internally associated with an integer constant by Al B. Tross for testing purposes, but the parrot sees only the 'x'.)

For instance, if the parrot were presented with the polynomial

x^3 + x + 11

the parrot might tap the following sequence of symbols:

x, *, x, *, x, +, x, +, 1, 1, =

The PDA has no extra memory, so each * or + operation is applied to the previous contents of the
display and whatever succeeding operand is entered. If the polynomial had been

x^3 + 2x^2 + 11

then the parrot would not have been able to "save" the value of x^3 while calculating the value of 2x^2. Instead, a different order of operations would be needed, for instance:

x, +, 2, *, x, *, x, +, 1, 1, =

The cost of a calculation is the number of key presses. The cost of computing x^3+x+11 in the example above is 11 (four presses of the x key, two presses of '*', 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.

You are to write a program that finds the least costly way for a parrot to compute a number of polynomial expressions. Because parrots are, after all, just bird-brains, they are intimidated by polynomials whose high-order coefficient is any value except 1, so this condition is always imposed.

Input

Input consists of a sequence of lines, each containing a polynomial and an x value. Each polynomial anx^n+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
 
  题目很难看懂,但理解了题目之后就很好做了。题目要求对多项式进行简化,使总的运算步骤最少,其实也就是利用秦九韶算法,将一元n次多项式的求值问题转化为n个一次式的算法:
  
 
 
把一个n次多项式
改写成如下形式:
秦九韶算法至今依旧是解决多项式求值得最优算法,题目要求最小运算步骤其实就是要求将多项式化成秦九韶形式后共有多少运算步骤,这样一来就很容易得出解答了。
// Problem#: 1040
// Submission#: 5061245
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
vector<int> coefficient;//系数容器,依次保存每个系数 
int n,an,x;//n为最高次数,an为x^n的系数,x为未知数的值 
int ans,cost;//ans保存多项式的结果,cost保存最小步骤 

int getDigit(int num)//用来计算一个int型整数的位数 
{
    int count=0;
    while(num)
    {
        num/=10;
        count++;
    }
    return count;
}

void Qinjiushao()
{

        coefficient.clear();
        int coe;
        for(int i=0;i<n;++i)//将除了an外的所有系数依次存入容器 
        {
            cin>>coe;
            coefficient.push_back(coe);
        }
        cin>>x;
        cost=0;
        ans=x;
        for(int i=0;i<coefficient.size()-1;++i)
        {
            if(coefficient[i])//如果系数不为0, 
            {
                cost+=getDigit(coefficient[i])+3;//则步骤增加系数的位数,系数前的+号增一,系数后的*号增一,*号后的x增一,所以又额外增加3 
            }
            else
            {
                cost+=2;//若系数为0,只有*号增一和*号后的x增一,所以增加2 
            }
            ans=(ans+coefficient[i])*x;//计算多项式的值 
        }
        if(coefficient[coefficient.size()-1])//若常数项不为0 
        {
            cost+=getDigit(coefficient[coefficient.size()-1])+3;//则步骤数要增加常数项的位数,+好增一,=号增一,然后先前未计算的最内层的x增一,所以再额外增加3 
        }
        else
        {
            cost+=2;//若常数项为0,则=号增一,最内层的x增一,共增加2 
        }
        ans+=coefficient[coefficient.size()-1];//求值的时候最后加上常数项 

}    

int main()
{
    int count=1;
    while(cin>>n>>an&&(n||an))
    {
        if(n)//如果最高次数不为0,则代入函数进行运算 
        {
            Qinjiushao();
            cout<<"Polynomial "<<count<<": "<<ans<<" "<<cost<<endl;
            count++;
        }
        else//若最高次数为0,则只有一个常数项,由于an必定为1,所有整个多项式只有一个常数项1 
        {
            cout<<"Polynomial "<<count<<": "<<an<<" "<<2<<endl;
        }
    
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值