No.3749 Three Jugs
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 -1For 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)$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。