How to find solutions of linear Diophantine ax + by = c? I want to find a set of integer solutions of Diophantine equation: $ax + by = c$, and apparently $\gcd(a,b)|c$. Then by what formula can I use to find $x$ and $y$ ? 
I tried to play around with it:
$x = (c - by)/a$, hence $a|(c - by)$.   
$a$, $c$ and $b$ are known. So to obtain integer solution for $a$, then $c - by = ak$, and I lost from here, because $y = (c - ak)/b$. I kept repeating this routine and could not find a way to get rid of it? Any hint?
Thanks,
Chan
 A: As others have mentioned one may employ the extended Euclidean algorithm. It deserves to be better known that this is most easily performed via row-reduction on an augmented matrix - analogous to methods used in linear algebra. See this excerpt from one of my old sci.math posts:
For example, to solve  mx + ny = gcd(x,y) one begins with
two rows  [m   1    0], [n   0    1], representing the two
equations  m = 1m + 0n,  n = 0m + 1n. Then one executes
the Euclidean algorithm on the numbers in the first column,
doing the same operations in parallel on the other columns,

Here is an example:  d =  x(80) + y(62)  proceeds as:

                      in equation form   | in row form
                    ---------------------+------------
                    80 =   1(80) + 0(62) | 80   1   0
                    62 =   0(80) + 1(62) | 62   0   1
 row1 -   row2  ->  18 =   1(80) - 1(62) | 18   1  -1
 row2 - 3 row3  ->   8 =  -3(80) + 4(62) |  8  -3   4
 row3 - 2 row4  ->   2 =   7(80) - 9(62) |  2   7  -9
 row4 - 4 row5  ->   0 = -31(80) +40(62) |  0 -31  40

Above the row operations are those resulting from applying
the Euclidean algorithm to the numbers in the first column,
        row1 row2 row3 row4 row5
namely:  80,  62,  18,   8,   2  = Euclidean remainder sequence
               |    |
for example   62-3(18) = 8, the 2nd step in Euclidean algorithm

becomes:   row2 -3 row3 = row4  on the identity-augmented matrix.

In effect we have row-reduced the first two rows to the last two.
The matrix effecting the reduction is in the bottom right corner.
It starts as the identity, and is multiplied by each elementary
row operation matrix, hence it accumulates the product of all
the row operations, namely:

       [  7 -9] [ 80  1  0]  =  [2   7  -9]
       [-31 40] [ 62  0  1]     [0 -31  40]

The 1st row is the particular  solution: 2 =   7(80) -  9(62)
The 2nd row is the homogeneous solution: 0 = -31(80) + 40(62),
so the general solution is any linear combination of the two:

       n row1 + m row2  ->  2n = (7n-31m) 80 + (40m-9n) 62

The same row/column reduction techniques tackle arbitrary
systems of linear Diophantine equations. Such techniques
generalize easily to similar coefficient rings possessing a
Euclidean algorithm, e.g. polynomial rings F[x] over a field, 
Gaussian integers Z[i]. There are many analogous interesting
methods, e.g. search on keywords: Hermite / Smith normal form, 
invariant factors, lattice basis reduction, continued fractions,
Farey fractions / mediants, Stern-Brocot tree / diatomic sequence.

A: Look can be deceiving. The integer solution to the equation $ax + by = c$ is anything but easy. Please endure a rather long derivation.
To make it more comprehensible let's first solve the equation for y:
\begin{align*}
ax + by = c\\
by = c - ax\\
y = \frac{c - ax}{b}
\end{align*}
To have an integer solution, $y$ must be an integer, and that is if $c - ax$ is a multiple of $b$, or $c - ax = -nb \iff ax = c + nb$. This has the same meaning as $ax \equiv c \: (mod \: n)$.
To continue, we need this Theorem 1:

The congruence $ax \equiv c \: (mod \: n)$ has a solution iff $gcd(a, n) \: | \: c$.

And this Lemma 2:

If $gcd(p, q) = 1$, then $px \equiv r \: (mod \: q)$ has a solution modulo $q$.

To keep this answer manageable, I would like to skip the proof of Theorem 1 and Lemma 2 (which can be found by googling). Just post a question and comment me if you encounter some trouble with them.
Let's define $d = gcd(a, n)$, and continue the derivation:
\begin{align*}
ax \equiv c \: (mod \: n)\\
ax = c + bn\\
\frac{a}{d} x = \frac{c}{d} + \frac{b}{d} n
\end{align*}
Now we want to switch $b$ and $n$ so $\frac{n}{d}$ could be seen more clearly as the modulo and continue it as following:
\begin{align*}
\frac{a}{d} x = \frac{c}{d} + b \frac{n}{d}\\
\frac{a}{d} x \equiv \frac{c}{d} \: (mod \: \frac{n}{d})
\end{align*}
Note that $\frac{a}{d}$ and $\frac{n}{d}$ from our derivation above is the $p$ and $q$ in the Lemma 2 respectively. Also note that as $d$ is $gcd(a, n)$, so $gcd(\frac{a}{d}, \frac{n}{d}) = 1$. Hence by Lemma 2:
\begin{align*}
\frac{a}{d} x \equiv \frac{c}{d} \: (mod \: \frac{n}{d})
\end{align*}
is our solution to equation $ax + by = c$.

As an example, let us solve $6x - 10y = 4 \iff 6x = 4 + 10y \iff 6x ≡ 4 \: (mod \: 10)$. $Gcd(6, 10) = 2$, and $2 \: | \: 4$, so by Theorem 1 that equation has a solution.
From our derivation, the solution is $\frac{6}{2} x = \frac{4}{2} \: (mod \: \frac{10}{2}) \iff 3x = 2 \: (mod \: 5)$.
By Lemma 2, we have a solution $modulo \: 5$. What it means is if we write the solution in $Z_5$, we would have:
\begin{align*}
\bar{3} \bar{x} = \bar{2}\\
\bar{x} = \bar{3}^{-1} \: \bar{2}
\end{align*}
As in $Z_5$, $\bar{3} \: \bar{2} = \bar{1} = \bar{3} \: \bar{3}^{-1}$, so $\bar{3}^{-1} = \bar{2}$ and we have:
\begin{align*}
\bar{x} = \bar{3}^{-1} \: \bar{2}\\
\bar{x} = \bar{2} \: \bar{2} = \bar{4}
\end{align*}
In $Z_5$, $\bar{x} = \bar{4} \iff x \equiv 4 \: (mod \: 5) \iff x = 4 + 5s \iff x = 4, 9, 13, \cdots$. You can check that indeed $x \equiv 4 \: (mod \:5)$ is the solution.
A: Here's another method. It doesn't require finding an initial solution, but it may require finding a modular multiplicative inverse. (Edit: I'll show this method with an example instead of a generalization. It may make it more clear.)
Solve $2x+3y=5$.
$2x\equiv 5\pmod{3}$.
You could either find a multiplicative inverse:
$x\equiv 5\cdot 2^{-1}\pmod{3}$
Or do it as follows:
$2x\equiv 2\pmod{3}$
Divide both sides by $2$ (notice $\gcd(3,2)=1$).
$x\equiv 1\pmod{3}$, $x=3n+1$, $n\in\mathbb Z$.
Substitute this value of $x$ into the original equation:
$2(3n+1)+3y=5$, $y=1-2n$.
Answer: $(x,y)=(3n+1,1-2n)$, $n\in\mathbb Z$.
A: Do you mean $\gcd(a,b)$ divides $c$? If so, you can divide both sides of the equation to get
$$
\frac{a}{g}x+\frac{b}{g}y=\frac{c}{g}
$$
where $g=\gcd(a,b)$.
But since $\gcd(a/g,b/g)=1$, you can use the extended Euclidean algorithm to find a solution $(x_0,y_0)$ to the equation
$$
\frac{a}{g}x+\frac{b}{g}y=1.
$$
Once you have that, the solution $(X,Y)=(\frac{c}{g}\cdot x_0,\frac{c}{g}\cdot y_0)$ is a solution to your original equation. Furthermore, the values
$$
x=X + \frac{b}{g} t\quad y=Y - \frac{a}{g} t
$$
give all solutions when $t$ ranges over $\mathbb{Z}$, I believe.
