题目如下:
题解 or 思路
首先我们需要知道:
对于:
s
u
m
=
1
+
2
+
3
+
4
+
.
.
.
+
n
sum = 1 + 2 + 3 + 4 + ... + n
sum=1+2+3+4+...+n
s
u
m
=
a
+
b
,
(
a
∈
[
0
,
s
u
m
]
)
sum = a + b, (a \in [0, sum])
sum=a+b,(a∈[0,sum])
这个在此就不再证明
于是我们可以列出等式
a
≤
b
a \le b
a≤b
x
+
a
=
y
+
b
x + a = y + b
x+a=y+b
a
+
b
=
s
u
m
a + b = sum
a+b=sum
化简:
x
−
y
+
s
u
m
=
2
∗
b
x - y + sum = 2 * b
x−y+sum=2∗b
所以
s
u
m
sum
sum 需要满足:
x
−
y
+
s
u
m
≥
0
x - y + sum \ge 0
x−y+sum≥0
(
x
−
y
+
s
u
m
)
m
o
d
2
=
0
(x - y + sum) \mod 2 = 0
(x−y+sum)mod2=0
所以暴力去找最小的 s u m sum sum 就是本题的答案。
AC 代码如下:
/*
Make it simple and keep self stupid
author:Joanh_Lan
*/
#pragma GCC optimize(3)
#pragma GCC optimize("inline") // 如果比赛允许开编译器优化的话,可以默写这两段
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <bitset>
#include <set>
#include <random>
#include <ctime>
#include <queue>
#include <stack>
#include <climits>
#define buff \
ios::sync_with_stdio(false); \
cin.tie(0);
#define int long long
#define ll long long
#define PII pair<int, int>
#define px first
#define py second
typedef std::mt19937 Random_mt19937;
Random_mt19937 rnd(time(0));
using namespace std;
const int mod = 1e9 + 7;
const int inf = 2147483647;
const int N = 100009;
//int Mod(int a,int mod){return (a%mod+mod)%mod;}
//int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
//int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
//int inv(int a,int mod){return qmi(a,mod-2,mod);}
//int lcm(int a,int b){return a*b/__gcd(a,b);}
int a, b;
void solve()
{
cin >> a >> b;
if (a > b)
swap(a, b);
if (a == b)
{
cout << 0 << '\n';
return;
}
int ans = 1;
for (int i = 1; ; i++)
{
int sum = (1 + i) * i / 2;
if (sum + a - b >= 0 && (sum + a - b) % 2 == 0)
{ ans = i; break;}
}
cout << ans << '\n';
}
signed main()
{
buff;
int _ = 1;
cin >> _;
while (_--)
solve();
}