RHandarov's blog

By RHandarov, history, 6 years ago, In English

Hi Codeforces, I have a problem where the solution is to solve diofant equation with K unknowns and K > 2. For example one equation of this type is: a1 * x + a2 * y + a3 * z = n and I have to find one solution of x, y and z. It is guaranteed that the equation always have a solution. Are there any algorithm that solve this problem? I will be thankful for helping :)

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Reduce it to the equation with 2 variables

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

A condition for a solution to exist: gcd(a1, a2, a3) divides n.

Now, if we rewrite the equation as: a1 * x + a2 * y = n - a3 * z, using the condition above, it shouldn't be hard to find z such that gcd(a1, a2) divides n - a3 * z. When you manage to do that, just solve the regular Diophantine equation left.