【CodeForces - 931A】【 Friends Meeting 】

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/82026156

题目:

Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = 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.

题意不是很难懂 ,不解释了哈。

解题思路:暴力就行,两点之间的距离如果为1的时候,直接移动一个就行,距离较大的时候,咱们同时移动两者,向中心靠拢,如果是奇数的话,一定要把最后的一步放在最后,理由数学常识。

ac代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#define maxn 10100
using namespace std;
int n,m;
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n>m)
		{
			int t=n;
			n=m;
			m=t;
		}
		int sum=0;
		int ant=1;
		while(n<m)
		{
			if(m-n==1)
			{
				sum+=ant;
				break;
			}
			n++;m--;
			sum+=2*ant++;	
		}
		printf("%d\n",sum);
	} 
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/82026156