SDUT 2022 Spring Team Contest(for 21) - 12 - Virtual Judge
JB is good at Math, so he thinks all the math problems in the world are easy.
But one day, he meets a math problem which he can't solve, so he asks you to help him.
JB will give you two numbers aa and bb, and you should then choose a positive odd number xx and a positive even number yy. You can let aa add xx or let aa minus yy in one operation. You should change aa into bb in the minimal number of operations. Note that you are not allowed to change the value of xx and yy.
Input
In the first line, there is one integer TT (1\leq T\leq 10^51≤T≤105), denoting the number of test cases.
For each test case, there is one line containing two numbers aa and bb (1\leq a,b\leq 10^61≤a,b≤106), denotes the number given by JB.
Output
For each test case, print one number, denoting the minimal number of operations you need to change aa into bb.
Sample 1
Inputcopy | Outputcopy |
---|---|
2 3 6 5 3 | 1 1 |
题意:给定两个正整数 a, b,你需要选定一个正奇数 x 和一个正偶 数 y。 之后的每一步操作中,你可以将 a 增大 x 或者将 a 减小 y。 求把 a 变成 b 的最少操作次数。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int a,b;
scanf("%d %d",&a,&b);
if(a==b)printf("0\n");
else if(a<b)
{
if((b-a)%2==1)printf("1\n");//这种好理解
else if((b-a)/2%2==1)printf("2\n");//类比第一种,b-a如果被分
//了两半能够为奇数,就是2,否则就是3
else printf("3\n");
//首先必须明确结果只可能为1,2,3;
}
else
{
//同理,结果只可能为1,2
if((a-b)%2==0)printf("1\n");
else
printf("2\n");
}
}
}