Description
Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x 1 = a, another one is in the point x 2 = b.
Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third — by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6.
The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point.
Input
The first line contains a single integer a (1 ≤ a ≤ 1000) — the initial position of the first friend.
The second line contains a single integer b (1 ≤ b ≤ 1000) — the initial position of the second friend.
It is guaranteed that a ≠ b.
Output
Print the minimum possible total tiredness if the friends meet in the same point.
Examples
Input |
---|
3 4 |
Output |
1 |
Input |
---|
101 99 |
Output |
2 |
Input |
---|
5 10 |
Output |
9 |
Note
In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1.
In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2.
In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend — two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
题意:两个人分别站在a1和a2,他们走一步可以移动1,花费是这样的:走第一步花费1,走第二步花费2,走第i步花费i…以此类推。问总共最少需要多少花费让两个人相遇。
思路:贪心,相遇的总步数是固定的,但前面的步数花费少,所有应该尽可能让两个人平摊步数。总步数分奇偶的情况,但使用一些技巧可以避免讨论。
代码:
#include<stdio.h>
int main() {
int a1=0, a2=0;
int d=0;
int b=0,ans=0;
scanf("%d %d", &a1, &a2);
d = a1 > a2 ? a1 - a2 : a2 - a1;
b = d / 2;
//b是两个人平摊的步数,即有两个1+2+...+b的和
//d&1是在处理d是奇数的情况,需要其中一个人多走一步,多一次b+1的花费
ans = b*(b+1) + (d & 1)*(b + 1);
printf("%d\n", ans);
}