Matching the Integral with a Riemann Sum

Given a polynomial, an interval [a,b], and N subintervals, find epsilon in [0, dx] where the Riemann sum equals the exact integral, or print -1.

Medium6MathBinary searchImplementationBrute forceNo attempts yetTime limit1sMemory limit512 MB

Problem

The integral of a function can be approximated as follows.

abf(x)dxk=0n1f(a+kΔx+ϵ)Δx\int_a^b f(x)\,dx \approx \sum_{k=0}^{n-1} f(a + k\Delta x + \epsilon)\,\Delta x

Δx=ban,0ϵΔx\Delta x = \frac{b-a}{n}, \qquad 0 \le \epsilon \le \Delta x

Mingyu was happily working through integrals at boot camp when he wondered whether a well-chosen ϵ\epsilon could make this approximation equal the exact integral. Checking that needs decimal arithmetic, which is hard to do by hand, so he asked you to write a program for it.

In this problem, ff is a polynomial.

Input

The first line contains a positive integer KK (1K101 \le K \le 10), the degree of the polynomial.

The second line contains the integer coefficients c1,c2,,cK+1c_1, c_2, \ldots, c_{K+1}, listed from the highest-degree term down to the constant term (0ci100 \le c_i \le 10, 1c1101 \le c_1 \le 10).

The third line contains two integers aa and bb, the start and end of the integration interval, and the number of subintervals NN (0a<b100 \le a < b \le 10, 1N101 \le N \le 10).

Output

Print on one line the value of ϵ\epsilon that makes the Riemann sum approximation equal the exact integral, rounded to exactly six digits after the decimal point. For example, if the answer is 0.250.25, print 0.250000.

For the given input, at most one ϵ\epsilon satisfies the condition. If no such value exists, print -1.

Hint

The integral of a polynomial term is computed as follows.

abxndx=bn+1an+1n+1\int_a^b x^n\,dx = \frac{b^{n+1} - a^{n+1}}{n+1}