题目:
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.
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
Input6 4 0 5 8 0 1000000 0 0 1 0 1000000 1000000Output
0 3 1000000 0 1 0
题目要求距离差为K最少步数,首先如果a等于k就不用移动,b可为a点,a小于k,就是k-a步,因为这时a移动到k,b可为0,最短。
如果a大于k,就判断a-k的差,为偶就不用移动,因为如果两数相减为偶,两数就只可能是1.奇奇2.偶偶,如果a为奇,中间可找到
任何一个点,相减使其为小于等于a的奇数,如果a为偶,中间可找到任何一个点,相减使其为小于等于a的偶数。
如果a-k的差为奇,那么a移动一位就变成差为偶的情况
代码:
#include<stdio.h> int main(){ int n,a,b; scanf("%d",&n); while(n--){ scanf("%d%d",&a,&b); if(a>b){ if((a-b)%2==0)printf("%d\n",0); else printf("%d\n",1); } else if(a<b)printf("%d\n",b-a); else printf("0\n"); } return 0; }