Share the Cakes

Time limit2sMemory limit128 MB

Summary
Given two disjoint convex polygons, find the single line that simultaneously bisects the area of both, and output its slope and intercept scaled by 1e6.
Level

Hard8 of 10

Topics
Geometry, Binary search, Divide and conquer, Math
Solved
No attempts yet

Problem

Lunar was born on the day of the Mid-autumn Festival, so every birthday she gets two cakes, a birthday cake and a moon cake.

This year Lunar wanted to share both cakes with her boyfriend Jaddy. She put the two cakes on the table and asked him to cut them, because she wanted half of the birthday cake and half of the moon cake. Jaddy took one lazy swing of the knife and cut through both cakes at once. Each cake ended up in two pieces, but neither cake was split into equal halves. Lunar got angry and left him.

Jaddy regretted it badly, so Lunar gave him one more chance. The restriction did not change. He may use only one cut, and that single cut has to divide both cakes into equal halves.

Both cakes are convex polygons. The table is an infinite plane and the blade is an infinite line. Find the line that bisects the area of both cakes at the same time.

Input

The first line contains the number of test cases TT (1≤T≤1001 \le T \le 100).

Each test case describes two cakes as two convex polygons. Each polygon starts with its number of vertices nn (3≤n≤203 \le n \le 20), followed by nn lines that give the coordinates xx and yy of the vertices in counterclockwise order. All coordinates are integers between −1000-1000 and 10001000.

The two polygons of a test case can be separated by a line, and no point of either polygon lies on that line.

Output

Print one line for each test case. Let y=kx+by = kx + b be the line Jaddy has to cut along, let KK be the integer nearest to k×106k \times 10^6, and let BB be the integer nearest to b×106b \times 10^6. Print the two integers together with the test case number ii in this format.

Case #i: K B

The numbering ii starts at 1.

In every test case exactly one line bisects both cakes, and that line is not parallel to the yy axis. Also ∣k∣≤10000|k| \le 10000 and ∣b∣≤10000|b| \le 10000, and each of k×106k \times 10^6 and b×106b \times 10^6 differs from its nearest integer by at most 0.450.45, so the rounding is never in doubt.

Examples2

  1. Example 1

    Input
    2
    3
    0 0
    1 1
    0 2
    3
    2 1
    3 0
    3 2
    4
    0 0
    1 0
    1 1
    0 1
    4
    2 2
    3 2
    3 3
    2 3
    
    Expected output
    Case #1: 0 1000000
    Case #2: 1000000 0
    
  2. Example 2

    Input
    2
    5
    -8 -5
    -4 -6
    -3 -6
    -5 0
    -8 2
    4
    2 -3
    3 -5
    6 0
    2 3
    3
    -7 -6
    -5 -6
    -4 3
    3
    5 4
    6 -6
    6 0
    
    Expected output
    Case #1: 203767 -1509605
    Case #2: 254754 -1981968