A and B CodeForces - 1278B math

Posted jacobfun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A and B CodeForces - 1278B math相关的知识,希望对你有一定的参考价值。

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
", ans);
	}
	return 0;
}

  

以上是关于A and B CodeForces - 1278B math的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 78 (Rated for Div. 2) B. A and B

[Codeforces#519E] A and B and Lecture Rooms

codeforces 416B. Appleman and Tree 树形dp

codeforces 623A. Graph and String 构造

[Codeforces 519B] A and B and Compilation Errors

Codeforces 519E A and B and Lecture Rooms [倍增法LCA]