Laser Shot

Time limit1sMemory limit128 MB

Summary
Find two different-direction bouncing laser paths from the droid to the Jedi, each with at most n bounces, that minimize the difference of their lengths.
Level

Hard8 of 10

Topics
Geometry, Math, Brute force, Implementation
Solved
No attempts yet

Problem

You are a droid trying to hit a Jedi standing inside a mirrored, square room. A Jedi can block a single laser with his lightsaber, so a direct shot is useless. Instead you fire two lasers so that they reach the Jedi at the same instant from two different directions — he cannot block both at once.

The walls are mirrors, so a laser can bounce off them, but the mirrors are imperfect: each laser can bounce at most nn times before it dissipates. You carry two laser pistols and have droid reflexes, so you may fire the two shots with an arbitrarily small gap between them (including firing both simultaneously). Because the lasers travel at a fixed speed, making them arrive together means the delay between the two shots equals the difference between the two path lengths; your goal is to make that delay as small as possible.

The room is a square 1,000,0001{,}000{,}000 feet on each side, with its lower-left corner at (0,0)(0, 0) and its upper-right corner at (1,000,000,1,000,000)(1{,}000{,}000, 1{,}000{,}000). You are at (x1,y1)(x_1, y_1) and the Jedi is at (x2,y2)(x_2, y_2). A laser that bounces around and later passes back through your position (x1,y1)(x_1, y_1) keeps going (you planned the shot and can dodge it), but a laser stops immediately the first time it reaches the Jedi at (x2,y2)(x_2, y_2). Reflections obey the usual rule — angle of incidence equals angle of reflection — and add no delay. A laser aimed exactly at a corner rebounds straight back along the direction it came from and counts as having bounced twice. The laser travels at 11 foot per nanosecond.

Two shots that arrive along the same final direction count as a single angle (the Jedi could block them together), so the two lasers must reach him along different final directions.

Input

The input contains several test cases. Each test case is a single line with five space-separated integers x1x_1, y1y_1, x2x_2, y2y_2, nn, where (x1,y1)(x_1, y_1) is your position, (x2,y2)(x_2, y_2) is the Jedi's position, and nn is the maximum number of bounces. The constraints are 1≤x1,y1,x2,y2≤999,9991 \le x_1, y_1, x_2, y_2 \le 999{,}999 and 1≤n≤1001 \le n \le 100, and the droid and the Jedi are never at the same location, i.e. (x1,y1)≠(x2,y2)(x_1, y_1) \ne (x_2, y_2). The line 0 0 0 0 00\ 0\ 0\ 0\ 0 marks the end of the input and must not be processed.

Output

For each test case, print on its own line the minimum possible delay, in nanoseconds, between firing the two lasers, rounded to exactly 55 decimal places. The test data guarantee that no answer lies within 10−610^{-6} of a rounding boundary.

Examples3

  1. Example 1

    Input
    100000 1 100000 999999 1
    100000 100000 800000 800000 1
    0 0 0 0 0
    
    Expected output
    19801.94156
    0.00000
    
  2. Example 2

    Input
    670488 116740 26226 777573 1
    0 0 0 0 0
    
    Expected output
    37350.01672
    
  3. Example 3

    Input
    848750 911528 6815 795668 2
    0 0 0 0 0
    
    Expected output
    6067.74748