Editorial for Đong gạo


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.

Công thức cơ bản

$$ \min \left\{ a + b \mid a, b \in \mathbb{Z}_{\geq 0},~5a + 3b = N \right\} = \min_{\substack{0 \leq a \leq \lfloor \frac{N}{5} \rfloor \\ }} \left\{ a + \frac{N - 5a}{3}|(N - 5a) \equiv 0 \pmod{3} \right\} $$

Đoạn giã code

for a = floor(N/5) down to 0:
    if (N - 5*a) mod 3 == 0:
        b = (N - 5*a)/3
        return a + b
return -1

Comments

Please read the guidelines before commenting.


There are no comments at the moment.