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+n−1] in that order. Klee wants to choose an index i i i ( 1 ≤ i ≤ n ) (1≤i≤n) (1≤i≤n) 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+⋯+ai−ai+1−⋯−an∣ 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) (1≤t≤104) — 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) (2≤n,k≤109) — 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=∣2−3∣=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+5−6−7∣=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;
}