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

import fractions
def myegcd(a,b):
    x1 = 1
    y1 = 0
    z1 = a
    x2 = 0
    y2 = 1
    z2 = b
    while z2!=fractions.gcd(a,b):
        if z2==0:
            break
        q = (z1-(z1%z2))/z2
        x1 = x1 -(q*x2)
        y1 = y1 -(q*y2)
        z1 = z1 -(q*z2)

        t = x1
        x1 = x2
        x2 = t

        t = y1;
        y1 = y2;
        y2 = t;

        t = z1;
        z1 = z2;
        z2 = t;
    return x2,y2

p,q = map(int,raw_input().split())
ans1,ans2 = myegcd(p,q)
print ans1,ans2