Calculator
Time limit1sMemory limit128 MB
Evaluate up to 100 polynomials given as compact strings with omitted coefficients at integer X and print each result.
- Level
Medium4 of 10
- Topics
- String, Math, Implementation
- Solved
- No attempts yet
Problem
The user feedback for your favourite open-source operating system is in, and the most requested feature turned out to be an extension of the built-in calculator. One of the suggested extensions is evaluating polynomials, and that is the part you are building.
You are given a polynomial that the user typed into the calculator together with a value for the variable. Evaluate the polynomial at that value.
Input
The first line contains , the number of polynomials. ()
Each test case consists of two lines. The first line contains an integer , the value of the variable the polynomial is evaluated at. ()
The second line contains a polynomial whose coefficients are all integers. is a sum of terms of the form , where the coefficient and the exponent satisfy the following conditions.
- is an integer with . If is 0, then is written as . If is 1, then is written as , except when is 1 or -1, in which case it is written as or .
- is an integer. If is 1 or -1 and is neither 0 nor 1, then is written as or .
- A + sign is written only in front of a term whose is non-negative and which is not the first term of the polynomial.
- Exponents in consecutive terms are strictly decreasing.
- fits in a 32-bit signed integer.
- The value of each term also fits in a 32-bit signed integer.
A polynomial line contains no spaces.
Output
For each test case, print the value of the polynomial on its own line. If the value for test case is , print it as Case #i: V. Test cases are numbered from 1. The result fits in a 32-bit signed integer.