#nt103. 扩展欧几里得2

扩展欧几里得2

Extended Euclidean 2

Problem Description

Read TT; there are TT groups of data. Each group gives three numbers a,b,da, b, d; output a non-negative integer solution (x,y)(x, y) of ax+by=dax+by=d. If there is no solution, output −1-1; otherwise output the solution with the smallest xx.

Input Format

The first line contains one number TT.

The following TT lines each contain three numbers a,b,da, b, d.

Output Format

TT lines in total, each with two numbers: x,yx, y.

Sample Input

3
1 2 5
100 150 1000
3 2 1

Sample Output

1 2
1 6
-1

Constraints

For 100%100\% of the data, it is guaranteed that 1≤T≤1041\leq T\leq 10^4, 1≤a,b≤1091\leq a, b\leq 10^9, 1≤d≤10181\leq d\leq 10^{18}.

Hint

  • Use the extended Euclidean algorithm to solve ax+by=gcd⁡(a,b)ax+by=\gcd(a,b). A solution to ax+by=dax+by=d exists iff gcd⁡(a,b)∣d\gcd(a,b)\mid d. To get the smallest non-negative xx, first find one solution then shift by multiples of b/gcd⁡b/\gcd (and a/gcd⁡a/\gcd for yy).