首页 > 其他 > 详细

A题题解

时间:2021-05-01 16:30:20      阅读:24      评论:0      收藏:0      [点我收藏+]

题目:

  

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 (1t60001≤t≤6000) — the number of test cases.

The only line of each test case contains two integers nn and kk (0n,k1060≤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
6
4 0
5 8
0 1000000
0 0
1 0
1000000 1000000
Output
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;
} 

 

A题题解

原文:https://www.cnblogs.com/TYoUer/p/14724119.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!