PROBLEM LINK:
Author:Pavel Sheftelevich
Tester:Vlad Mosko
Editorialist:Pawel Kacprzak
DIFFICULTY:
CAKEWALK
PREREQUISITES:
Simple math
PROBLEM:
You have $N$ coins. You are about to call any number of people between $1$ and $K$ and divide the coins to them in such a way that each person gets the same amount of coins and this amount is maximal. You take all coins left after this process. Your task is to compute the maximal number of coins which you can get.
QUICK EXPLANATION:
For each possible number of people, compute the amount of coins which are left after the process. Finally, return the maximum from these values.
EXPLANATION:
Let assume that you decided to call exactly $k$ people and you want to divide $N$ coins to them in the way described in the statement. It means, that each person gets the same amount of coins and this amount is maximal from all possible ones. You can simulate this process by giving one coin to all people at once until you have less than k coins, because then you cannot divide them equally. The number of coins which each person has after the process equals $N \mathbin{/} k$, where $\mathbin{/}$ is the integer division, i.e. $7 \mathbin{/} 3 = 2$. Based on this fact, it is easy to see that there are $r = N - k \cdot (N \mathbin{/} k)$ coins left after the process and this is the amount of coins which you get. Here $r$ is called a remainder after dividing $N$ by $k$ and it is very common in computation. Most programming languages have built in operation for computing a reminder. It is often represented by $\texttt{%}$ or $\texttt{mod}$.
Since $K$ is not so big, we can iterate over all possible values of it and for each of them, compute the remainder of $N \mathbin{/} k$, where k is the current number of people, and at the end print the maximum from these remainders.
Time complexity:
We are iterating over K values and for each of them we do constant time operations, so the total complexity is $O(K)$.
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here
Tester's solution can be found here