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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using SB = System.Text.StringBuilder;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
using System.Numerics;
using static System.Math;
using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        //SetIn(new StreamReader("in.txt"));
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        //SetOut(new StreamWriter("out.txt"));
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    readonly int dom = 998244353;
    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    void solve()
    {
        //WriteLine('1' + Enumerable.Repeat('9', 100000).concat());
        //WriteLine('1' + Enumerable.Repeat('9', 100000).concat());
        
        bool negative = false;
        var X = new Complex[2][];
        for (int j = 0; j < 2; j++)
        {
            var S = cin.next.reverse();
            if (S.Length == 1 && S[0] == '0')
            {
                WriteLine(0);
                return;
            }
            int flag = 0;
            if (S[S.Length - 1] == '-')
            {
                flag = 1;
                negative = !negative;
            }
    
            X[j] = new Complex[S.Length - flag];
            for (int i = 0; i < X[j].Length; i++)
            {
                X[j][i] = S[i] - '0';
            }
        }
        var F = new FastFourierTransform().Convolute(X[0], X[1]);
        var V = new int[F.Length];
        for (int i = 0; i < V.Length; i++)
        {
            V[i] = (int)Round(F[i].Real);
        }
        for (int i = 0; i < V.Length - 1; i++)
        {
            V[i + 1] += V[i] / 10;
            V[i] = V[i].ToString().Last() - '0';
        }
        if (negative) Write("-");
        bool z = false;
        for (int i = V.Length - 1; i >= 0; i--)
        {
            if (z || V[i] > 0)
            {
                Write(V[i]);
                z = true;
            }
        }
        WriteLine();
    }

}

class FastFourierTransform
{
    public FastFourierTransform() { }

    public Complex[] Convolute(Complex[] G, Complex[] H)
    {
        int sz = G.Length + H.Length + 1;
        int N = 1; while (N < sz) N <<= 1;
        Array.Resize(ref G, N);
        Array.Resize(ref H, N);

        FFT(G); FFT(H);
        Complex[] F = new Complex[N];
        for (int i = 0; i < N; i++)
        {
            F[i] = G[i] * H[i];
        }

        FFT(F, -1);
        return F;
    }
    void FFT(Complex[] F, int rev = 1)
    {
        for (int i = 0, j = 1; j < F.Length - 1; j++)
        {
            for (int k = F.Length >> 1; k > (i ^= k); k >>= 1) ;
            if (j < i) swap(ref F[i], ref F[j]);
        }

        for (int i = 2; i <= F.Length; i <<= 1)
        {
            int m = i >> 1;
            // zeta = exp(rev * PI / m * i)
            Complex zeta = new Complex(Cos(PI / m), Sin(PI / m) * rev);

            for (int j = 0; j < F.Length; j += i)
            {
                Complex zeta_pow = Complex.One;
                for (int u = j, v = j + m; v < j + i; u++, v++)
                {
                    Complex vl = F[u], vr = zeta_pow * F[v];
                    F[u] = vl + vr;
                    F[v] = vl - vr;
                    zeta_pow = zeta_pow * zeta;
                }
            }
        }

        if (rev == -1)
        {
            for (int i = 0; i < F.Length; i++)
            {
                F[i] /= F.Length;
            }
        }
    }

    void swap<T>(ref T a, ref T b)
    {
        var t = a;
        a = b;
        b = t;
    }
}

///// <summary>
///// <see cref="BaseWidth"/> * <see cref="MaxDigit"/> 桁に対応する多倍長整数
///// </summary>
//class BigInt
//{
//    static long Base = 1000;
//    const int BaseWidth = 3;
//    const int MaxDigit = 100000;
//    byte negative;
//    long[] digit = new long[MaxDigit];
//    int size;

//    public static BigInt Parse(string S)
//    {
//        BigInt num = new BigInt
//        {
//            negative = S[0] == '-' ? (byte)1 : (byte)0
//        };
//        int i = (S.Length - num.negative) % BaseWidth;
//        if (i > 0) i -= BaseWidth;
//        i += num.negative;
//        for (; i < S.Length; i += BaseWidth)
//        {
//            long ret = 0L;
//            for (int j = 0; j < BaseWidth; j++)
//            {
//                ret = 10 * ret + (i + j >= num.negative ? S[i + j] - '0' : 0);
//            }
//            num.digit[num.size++] = ret;
//        }
//        Array.Reverse(num.digit, 0, num.size);
//        return num;
//    }
//    static BigInt Add(BigInt x, BigInt y)
//    {
//        var ret = new BigInt { size = Max(x.size, y.size), negative = x.negative };
//        byte gain = 0;
//        for (int i = 0; i < ret.size; i++)
//        {
//            ret.digit[i] = x.digit[i] + y.digit[i] + gain;
//            if (ret.digit[i] >= Base)
//            {
//                ret.digit[i] -= Base;
//                gain = 1;
//            }
//            else
//            {
//                gain = 0;
//            }
//        }
//        if (gain == 1)
//        {
//            ret.digit[ret.size++] = 1;
//        }
//        return ret;
//    }
//    /// <summary>
//    /// x, yの絶対値の比較、差を求めたときのsize, negative
//    /// </summary>
//    /// <param name="x"></param>
//    /// <param name="y"></param>
//    /// <param name="size"></param>
//    /// <param name="negative"></param>
//    /// <returns></returns>
//    static int Compare(BigInt x, BigInt y, out int size, out byte negative)
//    {
//        int cmp = x.size.CompareTo(y.size);
//        if (cmp != 0)
//        {
//            size = cmp < 0 ? y.size : x.size;
//            negative = cmp < 0 ? y.negative : x.negative;
//            return cmp;
//        }
//        else
//        {
//            for (int i = x.size - 1; i >= 0; i--)
//            {
//                int diff = x.digit[i].CompareTo(y.digit[i]);
//                if (diff != 0)
//                {
//                    size = i + 1;
//                    negative = diff < 0 ? y.negative : x.negative;
//                    return diff;
//                }
//            }
//            size = 1;
//            negative = 0;
//            return 0;
//        }
//    }
//    static BigInt Sub(BigInt a, BigInt b)
//    {
//        var x = a;
//        var y = b;
//        int size; byte negative;
//        if (Compare(x, y, out size, out negative) < 0) { var tmp = x; x = y; y = tmp; }
//        var ret = new BigInt { size = size, negative = negative };
//        byte gain = 0;
//        for (int i = 0; i < ret.size; i++)
//        {
//            ret.digit[i] = x.digit[i] - y.digit[i] - gain;
//            if (ret.digit[i] < 0)
//            {
//                ret.digit[i] += Base;
//                gain = 1;
//            }
//            else
//            {
//                gain = 0;
//            }
//        }
//        while (ret.size > 1 && ret.digit[ret.size - 1] == 0) ret.size--;
//        return ret;
//    }
//    public static BigInt operator +(BigInt l, BigInt r)
//    {
//        if (l.negative == r.negative)
//        {
//            return Add(l, r);
//        }
//        else
//        {
//            return Sub(l, r);
//        }
//    }
//    public static BigInt operator -(BigInt l, BigInt r)
//    {
//        r.negative ^= 1;
//        return l + r;
//    }
//    static bool isZero(BigInt x)
//    {
//        return x.size == 1 && x.digit[0] == 0;

//    }
//    public static BigInt operator *(BigInt l, BigInt r)
//    {
//        var ret = new BigInt
//        {
//            size = l.size + r.size,
//            negative = l.negative != r.negative ? (byte)1 : (byte)0
//        };
//        if (isZero(l) || isZero(r))
//        {
//            ret.negative = 0;
//            ret.size = 1;
//            return ret;
//        }

//        var G = new Complex[l.size];
//        var H = new Complex[r.size];
//        for (int i = 0; i < G.Length; i++) G[i] = l.digit[i];
//        for (int i = 0; i < H.Length; i++) H[i] = r.digit[i];

//        var FFT = new FastFourierTransform().Convolute(G, H);

//        for (int i = 0; i < l.size + r.size; i++)
//        {
//            ret.digit[i] = (long)Round(FFT[i].Real);
//        }
//        long gain = 0;
//        for (int i = 0; i < ret.size; i++)
//        {
//            var tmp = ret.digit[i] + gain;
//            ret.digit[i] = tmp % Base;
//            gain = tmp / Base;
//        }
//        while (gain > 0)
//        {
//            ret.digit[ret.size++] = gain % Base;
//            gain /= Base;
//        }
//        while (ret.size > 1 && ret.digit[ret.size - 1] == 0) ret.size--;
//        return ret;
//    }
//    public override string ToString()
//    {
//        string ret = "";
//        if (negative == 1) ret += '-';
//        ret += digit[size - 1];
//        for (int i = size - 2; i >= 0; i--)
//        {
//            ret += digit[i].ToString().PadLeft(BaseWidth, '0');
//        }
//        return ret;
//    }
//}

static class Ex
{
    public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values));
    public static string concat<T>(this IEnumerable<T> values) => string.Concat(values);
    public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); }

    public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) <= 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}

class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f; public U s;
    public Pair(T f, U s) { this.f = f; this.s = s; }
    public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    public override string ToString() => $"{f} {s}";
}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan => ReadLine().Split();
    public int[] scanint => Array.ConvertAll(scan, int.Parse);
    public long[] scanlong => Array.ConvertAll(scan, long.Parse);
    public double[] scandouble => Array.ConvertAll(scan, double.Parse);
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint => int.Parse(next);
    public long nextlong => long.Parse(next);
    public double nextdouble => double.Parse(next);
}