B. A and B
题意
给两个数a.b,每次往a或者b上加一个值k,k初始为1,每加一次k加1,求使a,b相等需要的最少次数
如 a=1 b=3
a+=1=2
b+=2=5
a+=3=5
思路
很容易想到 1 3 6 10 15…这个序列,如果a,b差的绝对值为这个序列中的值的话,那么直接输出是第几个就好了
否则 比如 7这个数 只用3次必定是到不了的,那么四次呢?我们看一下四次的那个值10-7=3;这是个奇数,如果是个偶数的话那么就可以了,因为任意将前面一个数换为负的改变的是偶数,那么这时4次不行的,所以继续网上找直到 dis=21-7=14为偶数就可以了
/*
* @Author: JZlove
* @Date: 2019-12-19 22:55:35
* @Last Modified by: JZlove
* @Last Modified time: 2019-12-19 23:39:29
*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn=1e6+5;
LL a,b,val[maxn];
int cnt=1;
void pre()
{
while(true)
{
if(1LL*cnt*(1+cnt)/2>1e10+7)break;//预处理,记得开大点......血的教训
val[cnt]=1LL*cnt*(cnt+1)/2;
cnt++;
}
}
int main()
{
int T;
cin>>T;
pre();
while(T--)
{
cin>>a>>b;
LL dis=abs(a-b);
if(dis==0){cout<<0<<endl;continue;}
int p=lower_bound(val+1,val+cnt+1,dis)-val;
while((val[p]-dis)&1) p++;//依次往上找
cout<<p<<'\n';
}
return 0;
}