PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000276. Bài toán Euclid (Euclid Problem)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bài toán Euclid (Euclid Problem)

Euclid Problem

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Given positive integers A,BA,B, find integers X,Y,DX,Y,D such that AX+BY=DAX+BY=D and D=gcd⁡(A,B)D=\gcd(A,B). Among valid pairs, minimize ∣X∣+∣Y∣|X|+|Y|. If multiple pairs have the minimum, prefer the pair with X≤YX\le Y as specified in the original task.

Input

One line contains two positive integers A,BA,B.

Output

Print three space-separated integers X,Y,DX,Y,D.

Subtasks

  • Subtask 1 (20 points): 1≤A,B≤1001\le A,B\le 100.
  • Subtask 2 (30 points): 1≤A,B≤10000001\le A,B\le 1000000.
  • Subtask 3 (50 points): 1≤A,B≤100000000001\le A,B\le 10000000000.

Examples

Example 1

Input

4 6

Output

-1 1 2

Explanation

gcd⁡(4,6)=2\gcd(4,6)=2 and 4⋅(−1)+6⋅(1)=24\cdot(-1)+6\cdot(1)=2. The value ∣X∣+∣Y∣=2|X|+|Y|=2 is minimum among valid solutions.

Example 2

Input

17 17

Output

0 1 17

Explanation

gcd⁡(17,17)=17\gcd(17,17)=17 and 17⋅(0)+17⋅(1)=1717\cdot(0)+17\cdot(1)=17. The value ∣X∣+∣Y∣=1|X|+|Y|=1 is minimum among valid solutions.