Kangaroo Race
Time limit5sMemory limit1024 MB
Given a cyclic track of n segments and a kangaroo at segment x, find the fewest jumps of y(y-1) segments needed to reach segment 1, or report impossible.
- Level
Medium7 of 10
- Topics
- Math, Number theory
- Solved
- No attempts yet
Problem
The country of Austria is known for its large kangaroo population. To stay in good shape, each kangaroo has its own athletics track for practicing for the Annual Austrian Pogostick Jumping Event.
Each athletics track consists of segments, each meter long. The segments are numbered from to in order. The track is cyclic, so segment comes right after segment .
On each track, a kangaroo is located in one of the segments. The kangaroo can make a finite number of jumps. In each jump, if the kangaroo is in segment , it jumps segments ahead. Find the minimum number of jumps the kangaroo needs to reach segment .
Input
The first line contains an integer (), the number of kangaroos. Each of the next lines contains two integers and (), the number of segments on the track and the kangaroo's initial position on that track.
Output
For each kangaroo, if it can reach segment in a finite number of jumps, output the minimum number of jumps needed. Otherwise, output impossible.
Hint
Intermediate values in your calculation may exceed the range of 64-bit integers. To store them, use __int128 in C++ or java.math.BigInteger in Java or Kotlin. Python integers have arbitrary size by default, so no special handling is needed.