Under construction

Still being built. Things may change

Environment Protection

Time limit1sMemory limit128 MB

Summary
Given rational function boundaries of two underground layers, find the digging depth d so the exposed middle-layer area equals a target A, printed to five decimals.
Level

Hard8 of 10

Topics
Binary search, Math, Geometry, Implementation
Solved
No attempts yet

Problem

Arsenic & Cyanide Mining (ACM) is a corporation that has recently decided to start developing mines in the lands near your hometown. As a member of the citizens' regulatory committee for ACM's operations, your task is to control how much the corporation can mine from those lands, so that you keep the jobs and other benefits without sacrificing the environment or the health of the local residents.

ACM plans to mine several rectangular patches of land. A patch has width WW, can be dug down to a maximum depth DD, and has a flat surface which we consider to be at depth 00. The minerals in a patch are organized in three layers, which may vary in depth along the width of the patch but always keep the same profile along its whole length. This is why ACM cares only about the profile along the width of each patch, and has performed exploratory work to determine its shape precisely. The two interfaces between the three layers can be described by two functions y1(x)y_1(x) and y2(x)y_2(x): the first is the boundary between the top layer and the middle layer, and the second is the boundary between the middle layer and the bottom layer. These functions always satisfy

−D<y2(x)<y1(x)<0for 0≤x≤W,-D < y_2(x) < y_1(x) < 0 \qquad \text{for } 0 \le x \le W,

so the layer boundaries never touch. Each function has the form yi(x)=pi(x)/qi(x)y_i(x) = p_i(x)/q_i(x), where

pi(x)=∑k=0KPi,k xkandqi(x)=∑k=0KQi,k xk,p_i(x) = \sum_{k=0}^{K} P_{i,k}\, x^{k} \qquad \text{and} \qquad q_i(x) = \sum_{k=0}^{K} Q_{i,k}\, x^{k},

for i=1,2i = 1, 2 and a certain integer KK. Two example patches are described in the table below.

Example patchLayer interfaces
W=6W = 6, D=9D = 9y1(x)=−10+x2y_1(x) = \dfrac{-10 + x}{2}, y2(x)=−16+x2\quad y_2(x) = \dfrac{-16 + x}{2}
W=8W = 8, D=10D = 10y1(x)=−1392+864x−216x2+24x3−x41312−864x+216x2−24x3+x4y_1(x) = \dfrac{-1392 + 864x - 216x^2 + 24x^3 - x^4}{1312 - 864x + 216x^2 - 24x^3 + x^4}, y2(x)=−73+36x−54x2+36x3−9x417−4x+6x2−4x3+x4\quad y_2(x) = \dfrac{-73 + 36x - 54x^2 + 36x^3 - 9x^4}{17 - 4x + 6x^2 - 4x^3 + x^4}

ACM digs everything in a patch down to a certain digging depth dd and sells all the extracted minerals. However, the minerals in the top and bottom layers are essentially worthless, so the profit comes exclusively from the middle layer. In fact, the profit is proportional to the area AA of the part of the middle layer that lies at depth at most dd in the profile. Given the description of a patch and an integer AA, determine the digging depth dd you should allow so that ACM obtains an area of the middle layer in the profile equal to exactly AA. For the first example patch, obtaining area A=4A = 4 requires digging depth d=4.00000d = 4.00000; for the second example patch, obtaining area A=14A = 14 requires digging depth d=5.51389d = 5.51389.

Input

Each test case is described by five lines. The first line contains four integers WW, DD, AA and KK, where WW is the width of the patch (1≤W≤81 \le W \le 8), DD is its depth (1≤D≤101 \le D \le 10), AA is the required area of the middle layer in the profile (1≤A≤W×D1 \le A \le W \times D), and KK defines the interfaces y1(x)y_1(x) and y2(x)y_2(x) as explained above (0≤K≤80 \le K \le 8). Each of the next four lines contains K+1K + 1 integers between −108-10^{8} and 10810^{8}, inclusive. The second line gives the coefficients of p1(x)p_1(x) from P1,0P_{1,0} to P1,KP_{1,K}; the third line gives the coefficients of q1(x)q_1(x) from Q1,0Q_{1,0} to Q1,KQ_{1,K}; the fourth line gives the coefficients of p2(x)p_2(x) from P2,0P_{2,0} to P2,KP_{2,K}; the fifth line gives the coefficients of q2(x)q_2(x) from Q2,0Q_{2,0} to Q2,KQ_{2,K}.

The input contains several test cases; read until the end of input. Within each test case AA is strictly less than the total area of the middle layer in the profile, and there is a single value dd whose digging depth yields an area of exactly AA. Moreover, q1(x)≠0q_1(x) \ne 0, q2(x)≠0q_2(x) \ne 0, and −D<y2(x)<y1(x)<0-D < y_2(x) < y_1(x) < 0 for 0≤x≤W0 \le x \le W.

Output

For each test case, output one line with the digging depth dd that ACM should be allowed so that it obtains an area of exactly AA from the middle layer in the profile. Print dd as a decimal number with exactly five digits after the decimal point, rounded if necessary.

Examples5

  1. Example 1

    Input
    6 9 4 1
    -10 1
    2 0
    -16 1
    2 0
    8 10 14 4
    -1392 864 -216 24 -1
    1312 -864 216 -24 1
    -73 36 -54 36 -9
    17 -4 6 -4 1
    
    Expected output
    4.00000
    5.51389
    
  2. Example 2

    Input
    4 6 6 0
    -2
    1
    -5
    1
    
    Expected output
    3.50000
    
  3. Example 3

    Input
    6 9 6 1
    -10 1
    2 0
    -16 1
    2 0
    
    Expected output
    4.44949
    
  4. Example 4

    Input
    6 9 7 1
    -10 1
    2 0
    -16 1
    2 0
    
    Expected output
    4.64575
    
  5. Example 5

    Input
    4 6 7 0
    -2
    1
    -5
    1
    
    Expected output
    3.75000