Two trains and two flies

On the same rail there are two trains approaching each other. The train
at the left goes to the right at constant speed t₁. The train at the
right goes to the left at constant speed t₂. Initially, the noses of the
trains are d distance units apart. There is a fly at the nose of the
left train that starts flying to the right at constant speed f₁, where
f₁ > t₁. Similarly, there is a fly at the nose of the right train that
starts flying to the left at constant speed f₂, where f₂ > t₂. The flies
are so small that we can consider them as points. Any time that a fly
reaches another fly, or reaches a train, that fly turns around
immediately, never changing the absolute value of its speed. Thus, the
movement of each fly is like a zig-zag with an infinite number of
rebounds.

Given all the information, can you compute the total distance travelled
by each fly until the trains collide? If so, you would prove yourself
even better than von Neumann!

Input

Input consists of several cases, each one with d, t₁, t₂, f₁ and f₂. All
given numbers are strictly positive integers, and no larger than 10⁶.
Assume f₁ > t₁ and f₂ > t₂.

Output

For every case, print with four digits after the decimal point the total
distance travelled by the first fly and by the second fly. The given
cases have no precision issues.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T11:22:43.033Z

© Jutge.org, 2006–2026.
https://jutge.org
