B. A and B
題意:給A,B兩個數,通過操作:第i次操作將A或B加i。用最少的操作次數使A,B相等。
解析:
考慮等效思想,對A+i相當於對B-i。問題轉化爲1(+/-)2(+/-)3(+/-)…(+/-)n=A-B。大小等於等差數列到,n項的表達式奇偶性相等,因爲改變一個數前的符合後答案加上或減去這個數的二倍。
所以從1開始枚舉n,直到A-B在這個範圍內並且奇偶性相等時就是答案
#include<bits/stdc++.h>
using namespace std;
void solve(){
int a,b;
cin>>a>>b;
int x=max(a,b)-min(a,b);
if(x==0) {
cout<<"0"<<endl;
return ;
}
int ans=1;
while(1){
long long sum=1ll*(ans+1)*ans/2;
if(x<=sum&&sum%2==x%2){
cout<<ans<<endl;
return ;
}
++ans;
}
}
int main(){
int T;
cin>>T;
while(T--){
solve();
}
}