Chasing
InterviewTime limit1sMemory limit256 MB
Compute how many turns pass until a tortoise moving one section per turn and a hare moving two meet on a circular track.
- Level
Medium5 of 10
- Topics
- Math
- Solved
- No attempts yet
Problem
A tortoise and a hare chase each other on a circular track made of sections. The sections are numbered , and section comes right after section . The track is one way. From section the only move is to section , and from section the only move is to section .
When the race starts, the hare is on section and the tortoise is on section . A turn begins with the tortoise, which moves forward one section, and then the hare moves forward two sections. The race ends the moment both animals are on the same section, which can happen before the hare has finished its two sections. One turn counts the tortoise's move and the hare's move together. If the two are already on the same section before anyone moves, the race ends at once and takes turns.
Given the size of the track and the two starting sections, write a program that finds how many turns the race takes.
Input
The first line has the number of test cases ().
Each of the next lines has three integers , , (, ): the size of the track, the hare's starting section, and the tortoise's starting section of the -th test case.
Output
Print lines. The -th line has the number of turns the -th test case takes.