Problem:
Extended Euclid Algorithm

Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b.

Input

a b

Two positive integers a and b are given separated by a space in a line.

Output

Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily).

Constraints

1 ≤ a, b ≤ 109

Sample Input 1

4 12

Sample Output 1

1 0

Sample Input 2

3 8

Sample Output 2

3 -1

Solution in Scala (judge verdict: Accepted)

object Main {
  def main(args: Array[String]): Unit = {
    val Array(a, b) = io.StdIn.readLine().split(" ").map(_.toInt)
    val (x, y) = extendedEuclidean(a, b)
    println(s"$x $y")
  }
  def extendedEuclidean(a: Int, b: Int): (Int, Int) = {
    def loop(pr: Int, r: Int, ps: Int, s: Int, pt: Int, t: Int): (Int, Int) =
      if (r == 0) (ps, pt)
      else {
        val q = pr / r
        loop(r, pr-q*r, s, ps-q*s, t, pt-q*t)
      }
    loop(a, b, 1, 0, 0, 1)
  }
}