Under construction

Still being built. Things may change

Cutting Cake

Time limit45sMemory limit1024 MB

Summary
Given triangular icing patches on a cake, find the vertical cut that minimizes the absolute gap between the two pieces' enjoyment, reported as an exact fraction.
Level

Hard8 of 10

Topics
Geometry, Math, Sorting
Solved
No attempts yet

Problem

Today is the birthday of you and your twin sibling. To celebrate, you got a rectangular cake to share. The cake is decorated with NN triangular patches of icing (which may overlap). All the icing patches were made with the same triangular mold, so they have the same shape and orientation. Although you and your twin are very alike, your tastes in icing are very different. This difference is expressed by each of you having a different enjoyment value for each patch of icing. Specifically, your enjoyment for eating the entire ii-th patch is AiA_i, and your twin's is BiB_i. If someone eats part of a patch, they get enjoyment proportional to the eaten area. For example, if you eat 2/3 of the area of the ii-th patch, you get 2Ai3\frac{2A_i}{3} enjoyment from it. Some flavors of icing may be disliked by you or your twin, so AiA_i and/or BiB_i can be negative.

You will cut the cake into two rectangular pieces with a single vertical cut (parallel to the Y-axis). After cutting, you eat the left piece and your twin eats the right piece. Your total enjoyment is the sum of the enjoyment you get from all icing to the left of the cut. Similarly, your twin's total enjoyment is the sum of the enjoyment they get from all icing to the right of the cut. To be as fair as possible, you want to choose the cut so that the absolute difference between your total enjoyment and your twin's total enjoyment is as small as possible. Given the NN triangular icing patches on a rectangular cake, what is the minimum possible absolute difference between your and your twin's total enjoyment that you can get with a single vertical cut?

Input

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing three positive integers NN, WW, and HH: the number of icing patches on the cake, and the width and height of the top of the cake. The bottom-left corner of the cake is at (0,0)(0, 0) and the top-right corner is at (W,H)(W, H). Next comes a line describing the icing patch mold, with four integers PP, QQ, RR, and SS. The mold is a triangle with vertices at (0,0)(0, 0), (P,Q)(P, Q), and (R,S)(R, S). Then NN lines follow. The ii-th of these lines contains four integers XiX_i, YiY_i, AiA_i, and BiB_i. The ii-th patch is a triangle with vertices at (Xi,Yi)(X_i, Y_i), (Xi+P,Yi+Q)(X_i + P, Y_i + Q), and (Xi+R,Yi+S)(X_i + R, Y_i + S). Eating all of it gives you AiA_i enjoyment and your twin BiB_i.

Output

For each test case, output one line containing Case #x: y/z, where xx is the test case number (starting from 1) and y/zy/z is the minimum absolute difference between your and your twin's total enjoyment that can be achieved with a single vertical cut, written as an irreducible fraction. zz must be positive and as small as possible.

Hint

In Sample 1, there is a single icing patch. The optimal cut is to the left of the patch. You eat no icing and get 0 enjoyment. Your twin eats the entire patch and gets 5 enjoyment from it. The absolute difference is ∣0−5∣=5|0 - 5| = 5.

In Sample 2, there are two icing patches. The optimal cut is at X=15099999.99X = 15099999.99. The numerator and denominator of the answer can get very large.

In Sample 3, there are two icing patches. The optimal cut is at X=4X = 4. You eat 75% of the first patch and get −3-3 enjoyment from it. Your twin eats 25% of the first patch and all of the second patch, getting 5⋅0.25+5=6.255 \cdot 0.25 + 5 = 6.25 enjoyment. The absolute difference is ∣−3−6.25∣=9.25=374|-3 - 6.25| = 9.25 = \frac{37}{4}.

Cutting at X=1X = 1 gives you 0 enjoyment and your twin 10. Both values are larger than the ones at X=4X = 4, but the difference is 10, which is greater than 9.25, so X=4X = 4 is still better.

In Sample 4, there are three icing patches. The optimal cut is at X≈521241077.6027X \approx 521241077.6027.

Examples1

  1. Example 1

    Input
    4
    1 5 5
    3 -1 2 2
    1 2 -10 5
    2 100000000 50000000
    80000000 0 40000000 40000000
    5000001 2500000 500 -501
    15000000 5000000 501 -400
    2 10 10
    0 2 4 2
    2 2 -4 5
    4 6 -6 5
    3 622460462 608203753
    486076103 36373156 502082214 284367873
    98895371 126167607 823055173 -740793281
    26430289 116311281 -398612375 -223683435
    46950301 278229490 766767410 -550292032
    
    Expected output
    Case #1: 5/1
    Case #2: 288309900002019999899/320000000000000000
    Case #3: 37/4
    Case #4: 216757935773010988373334129808263414106891/187470029508637421883991794137967