Logo
(追記) (追記ここまで)

33734번 - Transforming Pairs 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB61343357.895%

문제

Bessie the brilliant bovine has discovered a new fascination—mathematical magic! One day, while trotting through the fields of Farmer John’s ranch, she stumbles upon two enchanted piles of hay. The first pile contains $a$ bales, and the second pile contains $b$ bales (1ドル\le a,b\le 10^{18}$).

Next to the hay, half-buried in the dirt, she discovers an ancient scroll. As she unfurls it, glowing letters reveal a prophecy:

To fulfill the decree of the Great Grasslands, the chosen one must transform these two humble hay piles into exactly $c$ and $d$ bales—no more, no less.

Bessie realizes she can only perform the following two spells:

  • She can magically conjure new bales to increase the first pile’s size by the amount currently in the second pile.
  • She can magically conjure new bales to increase the second pile’s size by the amount currently in the first pile.

She must perform these operations sequentially, but she can perform them any number of times and in any order. She must reach exactly $c$ bales in the first pile and $d$ bales in the second pile (1ドル\le c,d\le 10^{18}$).

For each of $T$ (1ドル\le T\le 10^4$) independent test cases, output the minimum number of operations needed to fulfill the prophecy, or if it is impossible to do so, output -1.

입력

The first line contains $T$.

The next $T$ lines each contain four integers $a,b,c,d$.

출력

Output $T$ lines, the answer to each test case.

제한

예제 입력 1

4
5 3 5 2
5 3 8 19
5 3 19 8
5 3 5 3

예제 출력 1

-1
3
-1
0

In the first test case, it is impossible since $b>d$ initially, but operations can only increase $b$.

In the second test case, initially the two piles have $(5, 3)$ bales. Bessie can first increase the first pile by the amount in the second pile, resulting in $(8, 3)$ bales. Bessie can then increase the second pile by the new amount in the first pile, and do this operation twice, resulting in $(8, 11)$ and finally $(8, 19)$ bales. This matches $c$ and $d$ and is the minimum number of operations to get there.

Note that the third test case has a different answer than the second because $c$ and $d$ are swapped (the order of the piles matters).

In the fourth test case, no operations are necessary.

예제 입력 2

1
1 1 1 1000000000000000000

예제 출력 2

999999999999999999

힌트

출처

Olympiad > USA Computing Olympiad > 2024-2025 Season > USACO 2025 February Contest > Silver 3번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /