POLEVAL - Evaluate the polynomial

Your task consists of evaluate a polynomial of degree n (0 <= n <= 999) represented by its n+1 coefficients of the form:

pn(x) = cnxn + cn-1xn-1 + … + c2x2 + c1x + c0

in each one of the k (1 <= k <= 100) points x1, x2, …, xk. The coefficients of the polynomial and the values where they will be evaluated are integers in the interval [-100, 100] that guarantees that the polynomial's evaluation is at the most 263 – 1.

Input

There will be multiple test cases, each one with 4 lines that are described below
n: degree of polynomial.
cn cn-1 … c2 c1 c0: coefficients of the polynomial separated by a single space.
k: number of points to evaluate the polynomial.
x1 x2 xk-1 xk: points to evaluate the polynomial separated by a single space.

The final test case is a single line where n = -1 and this case should not be processed.

Output

For each test case you should print k + 1 lines of output, the very first line containing the case number and the following k lines with the result of the polynomial's evaluation in each one of the k given points. See the sample.

Example

Input:
2
1 -2 -1
5
0 1 -1 2 -2
3
2 1 -2 -1
4
0 -1 2 -2
-1
Output: Case 1:
-1
-2
2
-1
7
Case 2:
-1
0
15
-9

Added by:Ivan Alfonso Olamendy
Date:2007-08-25
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:My own resource

hide comments
2020-04-22 16:31:32
AC in one Go!
My 50th...
2019-12-11 02:52:32
Possible with pure Python, albeit just at the limit (same solution that got AC, TLEs sometimes). Code too short to talk about any optimizations. Pythonists are advised to use PyPy here.
2018-07-09 12:19:12
Basically unsolvable with Python3, Horner + optimization = TLE
2017-11-30 18:00:10
simple implement


Last edit: 2017-11-30 18:01:25
2017-05-18 19:51:09
AC in one GO
Without Horner!!
2017-04-07 11:02:35
implement horner & get AC
2017-04-05 09:44:40
Naive approach works fine, AC in ONE GO!
2017-01-08 08:18:07
Using Naive approach got TLE but AC using Horner's method :)
2016-10-02 07:50:24
Simple yet very, very beautiful problem :D
2016-06-22 10:13:48
if you get TLE ,
google "Horner’s Method"
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.