Klee‘s SUPER DUPER LARGE Array!!!

Klee’s SUPER DUPER LARGE Array!!!

Problem

Klee has an array a of length n containing integers [ k , k + 1 , . . . , k + n − 1 ] [k,k+1,...,k+n−1] [k,k+1,...,k+n1] in that order. Klee wants to choose an index i i i ( 1 ≤ i ≤ n ) (1≤i≤n) (1in) such that x = ∣ a 1 + a 2 + ⋯ + a i − a i + 1 − ⋯ − a n ∣ x=|a_1+a_2+⋯+a_i−a_{i+1}−⋯−a_n| x=a1+a2++aiai+1an is minimized. Note that for an arbitrary integer z z z, ∣ z ∣ |z| z represents the absolute value of z z z.

Output the minimum possible value of x x x.

Input

The first line contains t t t ( 1 ≤ t ≤ 1 0 4 ) (1≤t≤10^4) (1t104) — the number of test cases.

Each test case contains two integers n n n and k k k ( 2 ≤ n , k ≤ 1 0 9 ) (2≤n,k≤10^9) (2n,k109) — the length of the array and the starting element of the array.

Output

For each test case, output the minimum value of x x x on a new line.

Example

Input
4
2 2
7 2
5 3
1000000000 1000000000
Output
1
5
1
347369930

Note

In the first sample, a = [ 2 , 3 ] a=[2,3] a=[2,3]. When i = 1 i=1 i=1 is chosen, x = ∣ 2 − 3 ∣ = 1 x=|2−3|=1 x=∣23∣=1. It can be shown this is the minimum possible value of x x x.

In the third sample, a = [ 3 , 4 , 5 , 6 , 7 ] a=[3,4,5,6,7] a=[3,4,5,6,7]. When i = 3 i=3 i=3 is chosen, x = ∣ 3 + 4 + 5 − 6 − 7 ∣ = 1 x=|3+4+5−6−7|=1 x=∣3+4+567∣=1. It can be shown this is the minimum possible value of x x x.

Code

// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <sstream>//整型转字符串
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    ll t;
    cin>>t;

    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        ll sum=(n+k)*(n+k-1)/2+k*(k-1)/2;
        
        ll ans=sqrt(sum);
        cout<<min(abs(sum-ans*(ans-1)),abs(sum-ans*(ans+1)))<<endl;
    }
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pretty Boy Fox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值