Loading

CF1278B A and B 题解

Problem

Solution

对样例换一种方式解释。

样例 \(1\)

\(a=1,b=3\to a=1+2,b=3\)

此时相等,故最小操作数为 \(2\)

样例 \(2\) 不用解释。

样例 \(3\)

\(a=30,b=20\to a=30,b=20+1+2+3+4\)

此时相等,故最小操作次数为 \(4\)

这些样例不明确,再构造一组样例:

\(a=5,b=22\to a=5+1+2+3+4+5+6=26>b=22\)

此时注意到若将 \(a\) 中的 \(2\) 转移到 \(b\) 上即可使 \(a=b\)

再构造一组样例:

\(a=5,b=23\to a=5+1+2+3+4+5+6=26>b=23\)

发现无法将 \(a\) 多出的部分均分,此时考虑继续加直至可以均分(可以证明最多加两次),然后多出部分均分即可。

推广至所有情况,发现只需使 \(a\)\(b\) 的差值小于 \(0\)(假设 \(a<b\),即要使 \(a-b>0\))和差值为偶数即可。

Code

#include<bits/stdc++.h>
using namespace std;
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define FOR(i,a,b) for(int i=(a);i>=(b);i--)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

int a,b;
void solve()
{
	cin>>a>>b;
	int c=abs(a-b);
	int i=0,sum=0;
	while(sum<c || abs(sum-c)%2==1)sum+=(++i);
	cout<<i<<"\n";
}
int main()
{
	IOS;
	int T;cin>>T;
	while(T--)solve();

	return 0;
}
posted @ 2024-08-13 21:54  ๑҉v  阅读(8)  评论(0)    收藏  举报