题解 CF1401A 【Distance and Axis】

Suuon_Kanderu

2020-08-22 09:21:06

Solution

自闭了自闭了,sb题也写不出来了。 我们容易发现,$\left|\operatorname{dist}(B,O) - \operatorname{dist}(A,B)\right|$ 的最大值是 n ,是 B 放在点 A 或 点 O 时的情况。所以说,如果 $k>n$ ,我们无论如何也无法找到点 B。只好把 A 移到 k 的位置,此时答案为 $k-n$。 然后咋搞啊?我选择暴力展开绝对值,方便快捷,无需找规律。 要求 $\left|\operatorname{dist}(B,O) - \operatorname{dist}(A,B)\right| = k$ 发现 $\operatorname{dist(B,O)} = B$ $$ \left|B - \operatorname{dist}(A,B)\right| = k$$ $$ \left|B - \left|A-B \right| \right| = k$$ 分情况讨论。 $$ \left|B - A+B \right| = k , \left|B + A-B \right| = k$$ $$B-A+B,-B+A-B,B+A-B,-B-A+B =k $$ $$2B-A = k, A-2B = k,A=k,A=k$$ $$B = \frac{k+A}{2}, B = \frac{A-k}{2},A=k$$ 可以看出第三种情况一定满足条件.因为 B 必须是整数点,所以如果 $\frac{k+A}{2}$ 和 $\frac{A-k}{2}$ 不是整数,我们只需要将 A 挪一下,B 就一定是偶数了。 代码非常简单,符合 CF 的风格。 ``` #include <cstdio> #include <iostream> #include <cmath> using namespace std; int main() { int t;scanf("%d" , &t); while(t--) { int a , b , k;scanf("%d%d" , &a , &k); if(k > a)printf("%d\n" , k - a); else if(a == k || (a - k) % 2 == 0 || (k + a)%2 == 0)puts("0"); else puts("1"); } } ```