RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0656

Transforming Pairs

Gold I 골드 I
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

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.

Problem credits: Benjamin Qi

제약

SCORING

  • Inputs 3-4: \(\max(c, d) \le 20 \cdot\min(a, b)\)
  • Inputs 5-7: \(T \le 10\) and \(a,b,c,d\le 10^6\)
  • Inputs 8-12: No additional constraints

Problem credits: Benjamin Qi

입력 형식

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
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
출력
999999999999999999
문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2024-2025 > February > Silver

태그

평가 및 의견

Transforming Pairs

개요
출제자 난이도 Gold I 골드 I 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Transforming Pairs

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8