题目1:求能满足式子
∑
i
=
1
x
(
−
1
)
y
∗
i
=
a
\sum_{i=1}^{x}(-1)^y*i=a
∑i=1x(−1)y∗i=a,且
y
y
y为0或1的
x
x
x的最小值。(首先全取
y
=
0
y=0
y=0满足
∑
i
=
1
x
≥
a
\sum_{i=1}^x \ge a
∑i=1x≥a,然后每次可以-2,-4,…,-2*x。即满足两个条件即可:
x
∗
(
x
+
1
)
/
2
≥
a
,
x
∗
(
x
+
1
)
/
2
−
a
为
偶
数
x*(x+1)/2 \ge a,x*(x+1)/2-a为偶数
x∗(x+1)/2≥a,x∗(x+1)/2−a为偶数)
#define int long long
int a, b;
int solve(int x) {
int l = 0, r = 1e9;
while (l < r) {
int mid = (l + r) >> 1;
if (mid * (mid + 1) / 2 >= a)
r = mid;
else
l = mid + 1;
}
//首先是要大于等于,然后就是要为偶数
while (1) {
int diff = l * (l + 1) / 2 - a;
if (diff % 2 == 0) break;
l++;
}
return l;
}
signed main() {
int T;
read(T);
while (T--) {
read(a), read(b);
a = abs(a - b);
int ans = solve(a);
// cout << ">>>>>>>";
cout << ans << endl;
}
return 0;
}
/*
Example
inputCopy
3
1 3
11 11
30 20
outputCopy
3
0
4
*/