五一训练礼包A-1
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?
OA长为n,设OB长i。
可在数轴上以A点为分界,分两类讨论。
1.当B在A左侧
可令OB为短,BA为长,则|OB|=i,|OA|=n,|AB|=n-i,
所以 k = |AB|-|OB| = n-2i ====> |OB| = i = (n-k)/2
要求B为整数,需要判(n-k)%2是否为0,是则存在B不需要操作,否则进行一次操作。
2.当B在A右侧
此时进行操作将A变化至K长,即操作k-n次存在B。
#include<stdio.h> int main(){ int t,n,k; scanf("%d",&t); while(t--){ scanf("%d %d",&n,&k); if(n>k){ if((n-k)%2==0) printf("0\n"); else printf("1\n"); }else printf("%d\n",k-n); } return 0; }
posted on 2021-05-05 19:30 wvellichor 阅读(73) 评论(0) 编辑 收藏 举报