#GL0000275. Phân phối kẹo (Candy Distribution)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phân phối kẹo (Candy Distribution)
Candy Distribution
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
There are children and each bag contains exactly candies. Buy a positive integer number of bags so that every child receives the same positive number of candies and exactly one candy remains. At most bags can be purchased.
Equivalently, find positive integers satisfying and . Output IMPOSSIBLE if no such pair exists; otherwise output any valid number of bags.
Input
One line contains two integers : the number of children and candies per bag.
Output
Print a valid integer , or IMPOSSIBLE if none exists.
Subtasks
- Subtask 1 (20 points): ; if a solution exists.
- Subtask 2 (30 points): ; if a solution exists.
- Subtask 3 (50 points): ; if a solution exists.
Examples
Example 1
Input
10 5
Output
IMPOSSIBLE
Explanation
, so has no integer solution.
Example 2
Input
10 7
Output
3
Explanation
For , the total candies are . Each child receives candies with one left over.
Example 3
Input
1337 23
Output
872
Explanation
For , the total candies are . Each child receives candies with one left over.
Example 4
Input
123454321 42
Output
14696943
Explanation
For , the total candies are . Each child receives candies with one left over.