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: Time Limit Exceeded)

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<stdio.h>
#include<stdlib.h>
//#define lli long long int
int gcd(int, int);
int main() {
	int a, b, c, i, j, x=0, y=0, sum,ab,flg=0;
	scanf("%d%d", &a,&b);
	c = gcd(a, b);
	sum = a + b;
	ab = a > b ? a : b;
	for (i = (-1)*ab; i <= ab; ++i) {
		for (j = (-1)*ab; j <= ab; ++j) {
			//if(flg&&i > j) continue;
			if (a*i + b*j == c) {
				if (abs(i) + abs(j) < sum) {
					sum = abs(i) + abs(j);
					x = i; y = j; flg = 1;
				}
			}
		}
	}
	printf("%d %d\n", x, y);
	return 0;
}

int gcd(int x, int y) {
	int tmp;
	if (x < y) { tmp = x; x = y; y = tmp; }
	if (x%y == 0) return y;
	gcd(x%y, y);
}