codeforces B. A and B to find the law

Educational Codeforces Round 78 (Rated for Div. 2)

1278B - 6

B. A and B 
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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).

Meaning of the questions: to give you two numbers a and b, you can choose a time and add to it a number n, the operation n ++, repeated several times, and finally make a == b, should you find the minimum number n.

At first I thought it was violent simulation questions, think of ways to use simulation, and actually WA. I gave up after the game actually hit the table to find the law? !

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(void)
{
	int t;
	ll a,b;
	scanf("%d",&t);
	//printf("%lld\n",ss);
	while(t--)
	{
		scanf("%lld %lld",&a,&b);
		//直接模拟
		ll n=0ll;
		ll sn=0;
		while(a!=b)
		{
			n++;//操作 
			if(a>b)
			swap(a,b);//a小b大
			sn=b-a;
			//差==n 
			if(n<=sn)
			a+=n;
			else
			b+=n;
			
		}
		//printf("a:%d b:%d\n",a,b);
		printf("%lld\n",n);
	}
	return 0;
}

  I have found, for example, input when a = 1 b 15 = output 12, i.e., when a + 36 = 47, b + 32 = 47. 7 should be the correct answer, i.e., a + 21 = 22, b + 7 = 22. It is wrong.

As for why the wrong, I do not know.

The following are playing table to find the law, not me looking, still do not understand.

Listen to me blind wave analysis, the above code is, I think, not just the minimum requirements. Examples of the above is that said set SUM [n] and the first n items, sum [7] = 28; 28-14 (that is, ab) = 14 (C is set, an even number, we can average 14 to a, b);

I.e., a = 1 + 14 = 15, b = 15; c = 14, divided equally to the c a, b i.e. a = 15 + 7 = 22, b = 15 + 7 = 22;

That is first a, b are changed to sum [n] -c, and then add the same number are changed to X, i.e. to ensure that (sum [n] -abs (ab)) is an even number.

It seems a little truth. If wrong welcome that

AC Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll sum[1000000+5];
int main(void)
{
	int t;
	ll a,b;
	scanf("%d",&t);
	for(int i=1;i<=1000000;i++)
	sum[i]=sum[i-1]+i;
	while(t--)
	{
		scanf("%lld %lld",&a,&b);
		ll n=0ll;
		ll sn=0;
		if(a!=b)
		for(int i=1;i<=1000000;i++){
			sn=abs(a-b);
			if(sum[i]>=sn&&(sum[i]-sn)%2==0)
			{
				n=i;
				break;
			}
		}
		printf("%lld\n",n);
	}
	return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/xuanmaiboy/p/12077124.html