Under construction

Still being built. Things may change

The Most Stable Water Level

Time limit1sMemory limit128 MB

Summary
Given a solid-of-revolution cup with expression-defined radius and wall thickness, compute the water fill level that minimizes the combined center of mass, requiring expression parsing, integration, and numerical optimization.
Level

Medium7 of 10

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

Problem

Physics tells us that a glass of water is most stable when it is neither completely full nor completely empty. A full glass is easy to knock over and spill; an empty one is light and tips just as easily; the best stability is usually reached somewhere in between. Your task is to find the water level that makes a cup as stable as possible.

We measure stability by the height of the cup's center of mass: the lower the center of mass, the more stable the cup. The center of mass is the mass-weighted average position of all the tiny particles that make up the glass and the water. Glass is about 2.52.5 times denser than water, so a glass particle has 2.52.5 times the mass of a water particle of the same size.

Every cup is a solid of revolution about a vertical axis, but its radius may change with height and the glass may vary in thickness. A cup is described by:

  • its height HH;
  • the thickness BB of its solid glass base;
  • a function R(x)R(x) giving the outer radius at height xx;
  • a function T(x)T(x) giving the glass thickness at height xx.

Height xx is measured from the bottom of the cup. From x=0x = 0 up to x=Bx = B the cup is solid glass (the base). From x=Bx = B up to x=Hx = H the glass forms a wall whose outer radius is R(x)R(x) and whose inner radius is R(x)−T(x)R(x) - T(x); the thickness T(x)T(x) is always measured horizontally. Water poured into the cup rests on top of the solid base and fills the inner cavity.

Input

The input contains several cup descriptions. Each description consists of three lines:

  • the first line contains two numbers HH (cup height) and BB (base thickness) with 0<B<H≤1000 < B < H \le 100;
  • the second line contains the expression R(x)R(x) (outer radius);
  • the third line contains the expression T(x)T(x) (glass thickness).

All values are given in centimeters. The list of descriptions ends with a line containing two zeros, which is not processed.

Each expression uses only the digits 0-9, decimal points (.), the four operators +, -, *, /, parentheses, and the lowercase letter x (the height, measured from the bottom of the cup). Operators have their usual meaning and precedence: * and / bind tighter than + and -.

The functions satisfy:

  • 0.1<R(x)≤1000.1 < R(x) \le 100 for every xx with 0≤x≤H0 \le x \le H;
  • 0.1≤T(x)<R(x)0.1 \le T(x) < R(x) for every xx with B≤x≤HB \le x \le H.

Output

For each cup, print one line in exactly this form:

Pour L litres / W cm of water.

Here LL is the volume of water, in litres, that must be poured in to make the cup as stable as possible, and WW is the corresponding water level: the height of the water column above the inner bottom of the cup (the top surface of the glass base). If the cup is most stable while empty, output 0.000 for both values.

Print LL and WW rounded to exactly three decimal places. All test data are chosen so that the exact values of LL and WW never fall on a rounding boundary, so the rounded output is unambiguous.

Examples5

  1. Example 1

    Input
    9 1
    3+x/6
    17/8-x/8
    10 1
    (x+10)
    10/(x+10)
    0 0
    
    Expected output
    Pour 0.030 litres / 3.365 cm of water.
    Pour 0.878 litres / 2.193 cm of water.
    
  2. Example 2

    Input
    20 2
    5
    1
    0 0
    
    Expected output
    Pour 0.306 litres / 6.083 cm of water.
    
  3. Example 3

    Input
    13 1
    4+x/13
    2/(1+x/10)
    0 0
    
    Expected output
    Pour 0.110 litres / 4.633 cm of water.
    
  4. Example 4

    Input
    30 3
    10-x/10
    2
    0 0
    
    Expected output
    Pour 1.368 litres / 8.185 cm of water.
    
  5. Example 5

    Input
    22 2
    4+x/11
    1.2
    0 0
    
    Expected output
    Pour 0.304 litres / 8.497 cm of water.