問題一覧 > 通常問題

No.3749 Three Jugs

レベル : / AI推定難易度 : 3250 / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 1024 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 2
作問者 : Naru820 / テスター : ponjuice
お気に入りにしたユーザー ProblemId : 14005 / 自分の提出
問題文最終更新日: 2026-10-31 15:08:02
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 515 (順位表) の他の問題:

Problem Statement

There are variables $a_1,a_2,a_3$. Initially, $(a_1,a_2,a_3) = (B_1,B_2,B_3)$. You may now perform the following operation any number of times.

  • Choose distinct integers $i,j \in \{1,2,3\}$. Simultaneously replace $a_i,a_j$ with $a_i - \min(a_i,A_j - a_j),a_j + \min(a_i,A_j - a_j)$.

Your goal is to make $(a_1,a_2,a_3) = (C_1,C_2,C_3)$. Determine whether this is possible, and if it is possible, find the minimum number of operations required to achieve the goal.

A total of $T$ test cases are given.

Constraints

  • $1 \le T \le 10^4$
  • $A_i \ge 1$
  • $A_1 + A_2 + A_3 \le 10^{18}$
  • $0 \le B_i,C_i \le A_i$
  • $B_1 + B_2 + B_3 = C_1 + C_2 + C_3$
  • All input values are integers.

Input

The input is given in the following format. Here, $\text{case}_i$ is the $i$-th test case.

$T$
$\text{case}_1$
$\text{case}_2$
$\vdots$
$\text{case}_T$

Each test case is given in the following format.

$A_1 \ A_2 \ A_3$
$B_1 \ B_2 \ B_3$
$C_1 \ C_2 \ C_3$

Output

Output $T$ lines.

On the $i$-th line, for $\text{case}_i$, output $-1$ if it is impossible to achieve the goal, and otherwise output the minimum number of operations required.

Sample

Sample 1
Input
4
3 2 1
3 0 0
1 1 1
3 2 2
3 0 0
1 1 1
499999999999999999 250000000000000000 249999999999999999
499999999999999999 0 0
374999999999999999 125000000000000000 0
400000000000000000 300000000000000000 200000000000000000
400000000000000000 0 0
0 200000000000000001 199999999999999999
Output
2
-1
499999999999999998
-1
For the first test case, the goal can be achieved with the following two operations.
  • Perform the operation with $i = 1,j = 2$. This gives $(a_1,a_2,a_3) = (1,2,0)$.
  • Perform the operation with $i = 2,j = 3$. This gives $(a_1,a_2,a_3) = (1,1,1)$.
The goal cannot be achieved with at most one operation, so the answer is $2$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。