Candy Distribution
Time limit1sMemory limit128 MB
For each pair K and C, find the smallest B up to 1e9 with B*C = K*X + 1 for some positive integer X, or report IMPOSSIBLE.
- Level
Medium5 of 10
- Topics
- Number theory, Math, Binary search, Implementation
- Solved
- No attempts yet
Problem
At a party with guests, the candy must be shared fairly, so the number of candies must be a multiple of - exactly candies for some positive integer . Because at least one guest always loses a candy, one extra candy is bought, making the total candies.
Candy is sold only in bags, and every bag contains exactly candies, so buying bags yields candies. A purchase of bags is valid when holds for some positive integer ; in particular the total number of candies is at least .
Input
The first line contains the number of test cases (). Each of the next lines contains two integers and separated by a space (). The buyer can never purchase more than bags, so only is allowed.
Output
For each test case, print the minimum number of bags that makes the purchase valid. If no valid number of bags exists within the allowed range, print IMPOSSIBLE instead. Print one answer per line.