码迷,mamicode.com
首页 > 其他好文 > 详细

A and B CodeForces - 1278B math

时间:2020-04-26 10:35:56      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:存在   one   force   div   seq   ons   ready   swa   cst   

You are given two integers aa and bb. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by 11; during the second operation you choose one of these numbers and increase it by 22, and so on. You choose the number of these operations yourself.

For example, if a=1a=1 and b=3b=3, you can perform the following sequence of three operations:

  1. add 11 to aa, then a=2a=2 and b=3b=3;
  2. add 22 to bb, then a=2a=2 and b=5b=5;
  3. add 33 to aa, then a=5a=5 and b=5b=5.

Calculate the minimum number of operations required to make aa and bb equal.

Input

The first line contains one integer tt (1t1001≤t≤100) — the number of test cases.

The only line of each test case contains two integers aa and bb (1a,b1091≤a,b≤109).

Output

For each test case print one integer — the minimum numbers of operations required to make aa and bb equal.

Example

Input
3
1 3
11 11
30 20
Output
3
0
4

Note

First test case considered in the statement.

In the second test case integers aa and bb are already equal, so you don‘t need to perform any operations.

In the third test case you have to apply the first, the second, the third and the fourth operation to bb (bb turns into 20+1+2+3+4=3020+1+2+3+4=30).

这道题首先得明白一点,从1加到x,使得a和b分别加c,d,能使得a+c==b+d,这样的c,d均是可以满足的,举例来说,我们从x开始,c -= x,如果此时c‘ > x - 1,则继续c’ -= x-1,如果某处y使得c‘‘ < y,那么 此时的c‘‘必定小于y,由于我们从x到y+1都用过了,y到1却没用过,而c‘‘在1到y之间,那么必定存在c‘‘没用过,所以构成c的数字有y+1到x和c‘‘之和,至于d,就是剩下的数字之和。

接着就是怎么找到满足要求的x,首先x*(x+1)/2得大于b-a(我们假设b>a),否则就算1到x全部给了a也不可能等于b,然后由于a最后变成a+c,b变成b+d,且两者相等,那么假设a+c=b+d=e,则c+d = 2e - 2a + a - b,因此c+d也就是x*(x+1)/2与a-b关于2同模,这也是要注意的。除此之外,就不需要其他条件了。

AC代码:

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a, b;
bool check(ll ans)
{
	ll res = ans * (ans + 1) / 2;
	if(res < b - a)
		return false;
	return res % 2 == (b - a) % 2;
}

int main()
{
	int t;

	ll ans;
	scanf("%d", &t);
	for(int i = 0; i < t; i++)
	{
		ans = 0;
		scanf("%lld %lld", &a, &b);
		if(b < a)
			swap(a,b);
		while(!check(ans))
			ans++;
		printf("%lld\n", ans);
	}
	return 0;
}

  

A and B CodeForces - 1278B math

标签:存在   one   force   div   seq   ons   ready   swa   cst   

原文地址:https://www.cnblogs.com/jacobfun/p/12777388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!