Editorial for CIW '26 P3 - Gumball Machine


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Kirito

The key observation is that the total number of gumballs dispensed equals AB. Initially, when A = B = 0, 0 gumballs are dispenesed.

When we press the A button, the total number of gumballs dispensed becomes AB + B = (A+1)B. Similarly, when we press the B buttom, the total number of gumballs dispensed becomes A + AB = A(B+1).

Thus we want to find integers A,B such that AB = N and A + B is minimized. Note that the smaller of A and B is at most \sqrt N, so by iterating through 1, 2, \ldots, \sqrt N and checking if they divide N, we can find the pair (A,B) which minimizes A + B.

Time Complexity: O(\sqrt N)


Comments

There are no comments at the moment.