#P9226. Candy Distribution Grouping
Candy Distribution Grouping
Candy Distribution Grouping
In Class 7 of the third grade, there are (n) students standing in a line from left to right. During the sports class, the teacher groups the students by counting them from left to right, and every time (k) students are counted, they are formed into a group. The teacher then gives a bag of candies to the last student of that group (i.e. the (k^{th}), (2k^{th}), (3k^{th}), (\ldots) students receive a bag of candy).
Meanwhile, the students from Class 6 heard about the candy distribution and they want to sneak into the end of the line (i.e. the very right side) in order to get one bag of candy for free. They want to know the minimum number of people that they must add to the end of the line so that at least one of them gets a bag of candies.
Assume that after the addition, the positions of the original Class 7 students remain unchanged and the newly added students occupy positions (n+1, n+2, \ldots, n+x). A student receives a bag of candy if their position is a multiple of (k). Find the smallest non-negative integer (x) such that there exists at least one multiple of (k) in the range ([n+1, n+x]).
Mathematically, if (r = n \bmod k), then the answer is given by: [ x = \begin{cases} k - r, & \text{if } r \neq 0, \ k, & \text{if } r = 0. \end{cases} ]
inputFormat
The input consists of two integers (n) and (k) separated by spaces on one line, where (n) is the number of students in Class 7 and (k) is the group size used during the counting.
outputFormat
Output a single integer representing the minimum number of additional students required to ensure that at least one student from Class 6 receives a bag of candy.
sample
5 3
1