We have a point AA with coordinate x=nx=n on OXOX-axis. We'd like to find an integer point BB (also on OXOX-axis), such that the absolute difference between the distance from OO to BB and the distance from AA to BB is equal to kk.
The description of the first test case.
Since sometimes it's impossible to find such point BB, we can, in one step, increase or decrease the coordinate of AA by 11. What is the minimum number of steps we should do to make such point BB exist?
Input
The first line contains one integer tt (1≤t≤60001≤t≤6000) — the number of test cases.
The only line of each test case contains two integers nn and kk (0≤n,k≤1060≤n,k≤106) — the initial position of point AA and desirable absolute difference.
Output
For each test case, print the minimum number of steps to make point BB exist.
Example
input
Copy
6
4 0
5 8
0 1000000
0 0
1 0
1000000 1000000
output
Copy
0
3
1000000
0
1
0
Note
In the first test case (picture above), if we set the coordinate of BB as 22 then the absolute difference will be equal to |(2−0)−(4−2)|=0|(2−0)−(4−2)|=0 and we don't have to move AA. So the answer is 00.
In the second test case, we can increase the coordinate of AA by 33 and set the coordinate of BB as 00 or 88. The absolute difference will be equal to |8−0|=8|8−0|=8, so the answer is 33.
https://codeforces.com/contest/1401/problem/A
题解:
讨论两种情况,设B的坐标为b,A的坐标为a。
当0<b<a时,要使满足条件的b存在,即|b-(a-b)|=k --> b=(a±k)/2<a --> a>k 。因此只有当a>k时,才存在点B在A左边,此时只要a+k是偶数即可使点B存在。
当b>a>0时,有|b-(b-a)|=|a|=k (b>a),即当且仅当a=k时,点B在点A右边存在
综上,当a>k时,我们只需要将A移动1或0次,使a+k成为偶数。当a<k时,将a移动到k处。
代码:
#include<iostream>
#include<cstdlib>
using namespace std;
int calc(int a,int b)
{
if ((a+b)%2==0)
{
return 0;
}
else if ((a+b)%2!=0)
{
return 1;
}
}
int main()
{
int n;
cin>>n;
for (int i=1;i<=n;i++)
{
int a,b,ans;
cin>>a>>b;
if (a<=b)
{
ans=int(b-a);
}
else
{
ans=calc(a,b);
}
cout<<ans<<endl;
}
}