#P10713. 【MX-X1-T1】「KDOI-05」简单的无限网格问题

【MX-X1-T1】「KDOI-05」简单的无限网格问题

Background

Original link: https://oier.team/problems/X1A

Problem Description

X is taking part in the KDOI Robot Championship.

The arena is an infinite grid. X's robot starts at (1,1)(1,1). He needs to perform several operations to move the robot to (n,m)(n,m) (n,m2n,m\ge 2).

In the ii-th operation, X may choose one of four directions: up, down, left, or right, and choose a positive integer xix_i, then make the robot move xix_i steps in that direction.

Unfortunately, X's robot has some bugs, so his operations must satisfy the following restriction, otherwise the robot will explode immediately:

  • For the ii-th operation, if ii is odd, then xix_i is also odd; if ii is even, then xix_i is also even.

Please help X compute the minimum number of operations needed to make his robot reach (n,m)(n,m).

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, the number of test cases.

For each test case, the input contains one line with two positive integers n,mn,m.

Output Format

For each test case, output one integer per line, representing the answer. It can be proven that X's robot can always reach (n,m)(n,m) in a finite number of steps.

3
8 7
999999 1000000
3 3
2
2
3

Hint

Sample Explanation

For the first test case, the robot can move as follows:

(1,1)(8,1)(8,7)(1,1)\to(8,1)\to(8,7)

A total of 22 operations are needed. It can be proven that there is no better sequence of operations.

Constraints

This problem uses bundled testdata.

Subtask ID Score n,mn,m\leq Special Property
11 3030 1010 None
22 10910^9 n,mn,m have the same parity
33 4040 None

For all data (100%100\%): 1T1051\leq T\leq10^5, 2n,m1092\leq n,m\leq10^9.

Translated by ChatGPT 5