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.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        long a = int.Parse(str[0]);
        long b = int.Parse(str[1]);
        long aa = 1;
        long ab = 0;
        long ba = 0;
        long bb = 1;
        while(b != 0){
            long ca = ba;
            long cb = bb;
            ba = aa - a/b*ba;
            bb = ab - a/b*bb;
            aa = ca;
            ab = cb;
            long c = b;
            b = a % b;
            a = c;
        }
        sb.Append(aa+" "+ab+"\n");
    }
}