B. A and B( 思維好題 )

 

const int N=2e6+5;

    int n,m,t;
    int i,j,k;
    int a[N];
    int sum[N];
    int num=0;
void init()
{
    for(i=1;;i++){
        sum[i]=sum[i-1]+i;
        if(sum[i]>1e9) break;
    }
    num=i-1;
}
void go(ll a,ll b)
{
    if(a==b){
        cout<<0<<endl;
        return ;
    }
    ll res=a-b;
    int pos;
    pos=lower_bound(sum+1,sum+1+num,res)-sum;
    if((sum[pos]-res)%2==0) cout<<pos<<endl;
    else{
        if((pos+1)%2==0) cout<<pos+2<<endl;
        else cout<<pos+1<<endl;
    }
}
int main()
{
    IOS;
    init();
    rush(){
        ll a,b;
        cin>>a>>b;
        if(b>a) swap(a,b);
        go(a,b);
    }
    PAUSE;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章