I have to find out the integral solution of a equation $ax+by=c$ such that $x \geq 0$ and $y \geq 0$ and value of $(x+y)$ is minimum.
I know if $c \equiv 0 \pmod{\gcd(a,b)}$ then it's always possible. How to find the values of $x$ and $y$?
I have to find out the integral solution of a equation $ax+by=c$ such that $x \geq 0$ and $y \geq 0$ and value of $(x+y)$ is minimum.
I know if $c \equiv 0 \pmod{\gcd(a,b)}$ then it's always possible. How to find the values of $x$ and $y$?
From the method of solving a linear Diophantene Equation we have that the solutions are $x = x_0 \pm n\cdot\frac{b}{(a,b)}$ and $y = y_0 \mp n \cdot \frac{a}{(a,b)}$, where the pair $x_0, y_0$ is a solution of the equation.
Now assume that $x+y \le x_0 + y_0$. If $x = x_0 + n\cdot\frac{b}{(a,b)}$, then $y = y_0 - n\cdot\frac{a}{(a,b)}$, this is equivalent to $b \le a$. If $x = x_0 - n\cdot\frac{b}{(a,b)}$, then $y = y_0 + n\cdot\frac{a}{(a,b)}$, this is equivalent to $a \le b$. So hence find one solution and if $a \le b$ then choose $n = \left\lfloor \frac{x_0(a,b)}{b} \right\rfloor$. Then $x = x_0 - n\cdot\frac{b}{(a,b)}$ and $y = y_0 + n\cdot\frac{a}{(a,b)}$ give you the minumum sum of $x+y$. Similarly if $b \le a$ then choose $n = \left\lfloor \frac{y_0(a,b)}{a} \right\rfloor$ so $x = x_0 + n\cdot\frac{b}{(a,b)}$ and $y = y_0 - n\cdot\frac{a}{(a,b)}$ will give you the minumum sum of $x+y$