目录
1.Problem
2.Input
3.Output
4.Examples
4.1input
4.2output
5.Code
6.Conclusion
1.Problem
William has two numbers aa and bb initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer kk is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer kk)
- add number kk to both aa and bb, or
- add number kk to aa and subtract kk from bb, or
- add number kk to bb and subtract kk from aa.
Note that after performing operations, numbers aa and bb may become negative as well.
William wants to find out the minimal number of operations he would have to perform to make aa equal to his favorite number cc and bb equal to his second favorite number dd.
2.Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.
The only line of each test case contains two integers cc and dd (0≤c,d≤109)(0≤c,d≤109), which are William's favorite numbers and which he wants aa and bb to be transformed into.
3.Output
For each test case output a single number, which is the minimal number of operations which William would have to perform to make aa equal to cc and bb equal to dd, or ?1?1 if it is impossible to achieve this using the described operations.
4.Examples
4.1input
6
1 2
3 5
5 3
6 6
8 0
0 0
4.2output
-1
2
2
1
2
0
5.Code
#include <bits/stdc++.h> using namespace std; int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int main() { int T = readint(); while (T--) { int x = readint(); int y = readint(); if (x % 2 != y % 2) { printf("-1 "); continue; } if (x == 0 && y == 0) { printf("0 "); continue; } if (x == y) { printf("1 "); } else { printf("2 "); } } return 0; }
6.Conclusion
这段代码的核心逻辑是解决一系列测试用例,每个测试用例包含两个整数 x 和 y。根据给定的条件,对每个测试用例进行以下判断和输出:
1.判断 x 和 y 是否具有相同的奇偶性(即都是偶数或都是奇数),如果奇偶性不相同,输出 -1,表示无解。
2.判断 x 和 y 是否都为零,如果是,输出 0,表示无需操作。
3.如果不满足上述两个条件,继续判断 x 是否等于 y,如果相等,输出 1,表示可以通过一次操作从 x 变为 y。
4.如果以上条件都不满足,输出 2,表示需要两次操作才能从 x 变为 y。总结来说,这段代码根据 x 和 y 的奇偶性和值的关系,输出相应的结果,即 -1、0、1 或 2。这是一个简单的条件判断和输出程序,用于处理一系列测试用例。