#P11062. 【MX-X4-T2】「Jason-1」加法

【MX-X4-T2】「Jason-1」加法

Background

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

Problem Description

Given two integers a,ba, b (possibly negative), you may perform any number of operations (including doing nothing). In each operation, you need to choose one of the following two forms:

  • Operation 1: Assign aa to the sum of aa and bb, i.e., aa+ba \gets a + b.
  • Operation 2: Assign bb to the sum of aa and bb, i.e., ba+bb \gets a + b.

Your goal is to minimize the absolute value of the difference between aa and bb, ab\lvert a-b \rvert. Output the minimum value.

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:

  • One line containing two integers a,ba, b.

Output Format

For each test case:

  • Output one integer on a single line, the answer.
5
1 1
3 7
-4 1
-5 -8
4 0

0
3
0
3
0

2
-6 9
34 -51

0
0

Hint

[Sample Explanation #1]

For the 1st test case, one feasible plan is: perform no operations, and ab=0\lvert a-b \rvert=0.

For the 2nd test case, one feasible plan is: first use operation 2, so bb is assigned to 1010; then use operation 1, so aa is assigned to 1313. At this time, ab=3\lvert a-b \rvert=3. It can be proven that this is the minimum value achievable.

For the 3rd test case, one feasible plan is: use operation 1 consecutively 55 times, and aa is assigned to 3,2,1,0,1-3,-2,-1,0,1 in order. At this time, aa and bb are equal, so ab=0\lvert a-b \rvert=0.

For the 4th test case, one feasible plan is: perform no operations, and ab=3\lvert a-b \rvert=3.

For the 5th test case, one feasible plan is: use operation 2, so bb is assigned to 44. At this time, ab=0\lvert a-b \rvert=0.

[Sample Explanation #2]

For both test cases in this sample, you can first use operation 2 once, then use operation 1 three times, making the absolute difference 00.

[Constraints]

Test Point ID Special Property Score
1 A 2727
2 B 3131
3 None 4242
  • Special Property A: a,b10\lvert a \rvert, \lvert b \rvert \le 10 is guaranteed.
  • Special Property B: a,b1a,b \ge 1 is guaranteed.

For 100%100\% of the testdata, 1T1051 \le T \le 10^5, a,b109\lvert a \rvert, \lvert b \rvert \le 10^9.

Translated by ChatGPT 5