Let p[0… n] be a vector of integer numbers that contains the coefficients
of a polynomial of degree n≥0. For instance, the vector
p=⟨3,2,5,−1⟩ represents p(x)=3+2x+5x^{2}−x^{3}, a polynomial of
degree n=3.

Write a function

int evaluate(const vector<int>& p, int x);

that evaluates the polynomial at the point *x*,
that is, that returns
∑_{i=0}^{n} p[i]x^{i}.

Use the Horner scheme:

p_{n}x^{n}+p_{n−1}x^{n−1}+…+p_{0}=((p_{n}x+p_{n−1})x+…)x+p_{0}. |

Observation You only need to submit the required procedure; your main program will be ignored.

Information

- Author
- Jordi Petit
- Language
- English
- Translator
- Carlos Molina
- Original language
- Catalan
- Other languages
- Catalan
- Official solutions
- C++
- User solutions
- C++