codeforces 931 A. Friends Meeting

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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值