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 Ruby (judge verdict: Accepted)

# Param: Integer a
# Param: Integer b
# Return: (u, v, d) s.t. a*u + b*v = gcd(a, b) = d
def extended_gcd(a, b)
	u0 = 1
	u1 = 0

	return u0, u1, a if b.zero?

	d0 = a	# d0 = a * u0 + b * v0
	d1 = b	# d1 = a * u1 + b * v1

	loop do
		q, r = d0.divmod(d1)
		return u1, (d1 - a * u1) / b, d1 if r.zero?

		d0, d1 = d1, r
		u0, u1 = u1, u0 - q * u1
	end
end

puts extended_gcd(*gets.split.map(&:to_i)).first(2).join(' ')