Clock

Time limit2sMemory limit128 MB

Summary
Given hands rotating at nested integer-ratio speeds, choose how much to turn each hand (dragging slower ones) to move from one timestamp to another minimizing total handle travel, output the exact minimal distance as a reduced fraction.
Level

Medium7 of 10

Topics
Math, Greedy, Number theory
Solved
No attempts yet

Problem

A celebrated architect is designing a monumental clock that displays the time elapsed since the beginning of the universe.

The clock has nn hands that turn at constant speeds, numbered from 11 to nn, fastest to slowest. Hand 11 completes one full revolution every minute (6060 seconds). Each hand turns slower than the one before it: while hand ii makes did_i full revolutions, hand i+1i+1 makes exactly one.

To set the clock you grab a hand by the handle at its tip and turn it either way. Turning a hand drags every slower hand along in proportion to its usual speed, while every faster hand stays still. The hands are enormous, so the effort spent equals the total distance travelled by the handles you grab.

Consider three hands — a second hand, a minute hand, and an hour hand of lengths 55, 1515, and 1010 meters. To set the clock from 2:30 to 6:00 (see the figure) the cheapest way is to turn the minute hand 180°180° clockwise and then the hour hand 90°90° clockwise; the handles travel a total of about 62.8362.83 meters.

Setting the clock from 2:30 to 6:00.

Find a way to set the clock that minimizes the total distance the handles travel.

Input

The first line contains one integer nn — the number of hands (0<n≤500 < n \le 50).

The second line contains n−1n-1 integers d1,d2,…,dn−1d_1, d_2, \ldots, d_{n-1} (2≤di≤1062 \le d_i \le 10^6); when n=1n = 1 this line is empty.

The third line contains nn integers l1,l2,…,lnl_1, l_2, \ldots, l_n (1≤li≤1061 \le l_i \le 10^6) — the lengths of the hands.

The next two lines each contain one non-negative integer: the time the clock currently shows and the time it must be set to. Both times are measured in seconds and are less than 2632^{63}.

Output

Turning a hand of length ll through one full revolution moves its handle a distance of 2πl2\pi l, so the minimal total distance is always 2πQ2\pi Q for a rational number QQ — the sum, over all hands, of a hand's length multiplied by the number of revolutions it is turned.

Output QQ as a reduced fraction p/q with q>0q > 0 and gcd⁡(∣p∣,q)=1\gcd(|p|, q) = 1 (print 0/1 when no hand has to move).

In the example above the minimal distance is 20π=2π⋅1020\pi = 2\pi \cdot 10, so the answer is 10/1.

Examples4

  1. Example 1

    Input
    3
    60 12
    5 15 10
    52200
    453600
    
    Expected output
    10/1
    
  2. Example 2

    Input
    1
    
    7
    1000
    1000
    
    Expected output
    0/1
    
  3. Example 3

    Input
    1
    
    10
    0
    30
    
    Expected output
    5/1
    
  4. Example 4

    Input
    2
    4
    3 5
    0
    95
    
    Expected output
    3/1