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

using System;
using System.Linq;

namespace NTL_1_E
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] x = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int a0 = 1; int a1 = 0;
            int b0 = 0; int b1 = 1;
            while (x[1] > 0)
            {
                int q1 = x[0] / x[1];
                int r2 = x[0] % x[1];
                int a2 = a0 - q1 * a1;
                int b2 = b0 - q1 * b1;
                x[0] = x[1]; x[1] = r2;
                a0 = a1; a1 = a2;
                b0 = b1; b1 = b2;
            }
            Console.WriteLine(a0 + " " + b0);
        }
    }
}