Limits 1s, 512 MB

The famous astronaut Roddur Roy has reached an unknown planet by losing his way. Now he needs a lot of energy to restart his rocket and come back. Sadly, sunlight does not reach on this planet. But there is a strange intense light source, and he has a machine to collect energy from light.

He used a rectangular mirror of four walls to reduce the intensity of light. Mr Roddur calculated that the ray becomes useful after reflecting exactly $K$ times on the wall. He notices that the speed of producing energy increases if light travels less. To minimize the distance, he adjusts the direction of source light. But he is unable to calculate the total distance. Can you help him find the shortest distance the light has to travel to reach the machine?


  • Consider that the surface is two-dimensional.
  • If the light goes to a corner, it will move back in the exactly opposite direction with 2 reflection.
  • Light can pass through the machine before/after reflecting K times.
  • For further details on reflection go [here](


The first line contains a positive integer $T (T \leq 1000)$ — the number of test cases.

Each test case contains $7$ integers — $K, A, B, x_1, y_1, x_2, y_2 (0 \leq K \leq 1000, 2 \leq A \leq 1000, 2 \leq B \leq 1000, 0 < x_1 < A, 0 < y_1 < B, 0 < x_2 < A, 0 < y_2 < B)$, representing the number of reflection ($K$), size of the rectangle (length $A$, width $B$), position of source light $(x_1,y_1)$, position of machine $(x_2,y_2)$.


For each test case, output a single line consisting of the shortest distance the light will travel, corrected to 2 decimal places.


1 100 100 1 1 1 1
2 100 3 50 1 50 2
2 100 100 1 1 1 1


Login to submit.


100% Solution Ratio
EgorKulikovEarliest, Apr '20
nusuBotFastest, 0.0s
MamnoonSiamLightest, 131 kB
tasmeemrezaShortest, 864B
Toph uses cookies. By continuing you agree to our Cookie Policy.