AT_arc222_f.Triple Transformation

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Consider an operation on a triple (x,y,z)(x,y,z) of non-negative integers. In the operation, (x,y,z)(x,y,z) is replaced according to the following rules:

  • If y+z<xy+z<x: replace with (x−y−z, 2y, 2z)(x-y-z,\ 2y,\ 2z).
  • If x+z<yx+z<y: replace with (2x, y−x−z, 2z)(2x,\ y-x-z,\ 2z).
  • If x+y<zx+y<z: replace with (2x, 2y, z−x−y)(2x,\ 2y,\ z-x-y).
  • If none of the above conditions hold: replace with (y+z−x, x+z−y, x+y−z)(y+z-x,\ x+z-y,\ x+y-z).

Note that every triple of non-negative integers falls into exactly one of the above four cases.

You are given non-negative integers A1,A2,A3,B1,B2,B3A_1, A_2, A_3, B_1, B_2, B_3. Consider performing the operation on the triple (A1,A2,A3)(A_1,A_2,A_3) zero or more times to obtain (B1,B2,B3)(B_1,B_2,B_3). Find the minimum number of operations required to do so. If it is impossible to obtain (B1,B2,B3)(B_1,B_2,B_3) no matter how many times the operation is performed, output -1.

TT test cases are given; solve each of them.

考虑对一个由非负整数构成的三元组 (x,y,z)(x,y,z) 进行一种操作。该操作按照以下规则将 (x,y,z)(x,y,z) 替换为一个新的三元组:

  • 若 y+z<xy+z<x:替换为 (x−y−z, 2y, 2z)(x-y-z,\ 2y,\ 2z);
  • 若 x+z<yx+z<y:替换为 (2x, y−x−z, 2z)(2x,\ y-x-z,\ 2z);
  • 若 x+y<zx+y<z:替换为 (2x, 2y, z−x−y)(2x,\ 2y,\ z-x-y);
  • 若以上条件均不满足:替换为 (y+z−x, x+z−y, x+y−z)(y+z-x,\ x+z-y,\ x+y-z)。

注意:任意一个非负整数三元组必恰好属于上述四种情况之一。

给定非负整数 A1,A2,A3,B1,B2,B3A_1, A_2, A_3, B_1, B_2, B_3。考虑对三元组 (A1,A2,A3)(A_1,A_2,A_3) 执行零次或多次该操作,使其变为 (B1,B2,B3)(B_1,B_2,B_3)。求实现这一目标所需的最少操作次数。若无论执行多少次操作都无法得到 (B1,B2,B3)(B_1,B_2,B_3),则输出 -1。

共给出 TT 组测试用例,请对每组分别求解。

输入格式

The input is given from Standard Input in the following format:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

Each test case is given in the following format:

A1A_1 A2A_2 A3A_3 B1B_1 B2B_2 B3B_3

输入从标准输入中按以下格式给出:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

每个测试用例按以下格式给出:

A1A_1 A2A_2 A3A_3 B1B_1 B2B_2 B3B_3

输出格式

Output one line per test case.

For each test case, output the minimum number of operations required to obtain (B1,B2,B3)(B_1,B_2,B_3) by performing the operation on the triple (A1,A2,A3)(A_1,A_2,A_3) zero or more times. If it is impossible to obtain (B1,B2,B3)(B_1,B_2,B_3) no matter how many times the operation is performed, output -1.

每个测试用例输出一行。

对于每个测试用例,输出通过零次或多次对三元组 (A1,A2,A3)(A_1,A_2,A_3) 执行该操作,得到 (B1,B2,B3)(B_1,B_2,B_3) 所需的最少操作次数。如果无论执行多少次操作都无法得到 (B1,B2,B3)(B_1,B_2,B_3),则输出 -1。

输入输出样例

  • 输入#1

    6
    2 3 4 2 3 4
    2 3 4 5 3 1
    2 3 4 1 6 2
    2 3 4 4 3 2
    1 0 0 0 0 1
    0 0 0 0 0 0

    输出#1

    0
    1
    2
    -1
    -1
    0

说明/提示

Sample 1 Explanation:
For (A1,A2,A3)=(2,3,4)(A_1,A_2,A_3)=(2,3,4), the triple changes as follows when the operation is repeatedly applied:

  • (2,3,4)→(5,3,1)→(1,6,2)→(2,3,4)→(5,3,1)→(1,6,2)→⋯(2,3,4)\to (5,3,1)\to (1,6,2)\to (2,3,4)\to (5,3,1)\to (1,6,2)\to\cdots

From this, we can see that the answers to the first three test cases are 0,1,20, 1, 2.

Constraints

  • 1≤T≤3001\leq T\leq 300
  • 0≤A1,A2,A3,B1,B2,B3≤1080\leq A_1, A_2, A_3, B_1, B_2, B_3\leq 10^8
  • All input values are integers.

样例 1 解释:
对于 (A1,A2,A3)=(2,3,4)(A_1,A_2,A_3)=(2,3,4),反复应用该操作时,三元组的变化如下:

  • (2,3,4)→(5,3,1)→(1,6,2)→(2,3,4)→(5,3,1)→(1,6,2)→⋯(2,3,4)\to (5,3,1)\to (1,6,2)\to (2,3,4)\to (5,3,1)\to (1,6,2)\to\cdots

由此可知,前三个测试用例的答案分别为 0, 1, 20,\ 1,\ 2。

约束条件

  • 1≤T≤3001\leq T\leq 300
  • 0≤A1,A2,A3,B1,B2,B3≤1080\leq A_1, A_2, A_3, B_1, B_2, B_3\leq 10^8
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页