跳到正文

题解

题解:P1001 A+B Problem

AFO_Lzx约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年5月10日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P1001

版本

  1. v1当前

这辈子都没想到可以写 A+B 的题解,这一眼就是二分答案。

注意到 ∣a∣,∣b∣≤109\vert a \vert,\vert b \vert \le 10^9,所以二分的范围为 [−2×109,2×109][-2 \times 10^9,2\times10^9]。直接套板子即可。

C++
#include<bits/stdc++.h>
using ll = long long;
using namespace std;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int a, b;
	cin >> a >> b;
	
	int l = -2e9, r = 2e9;
	while (l < r) {
		int mid = (l + r) >> 1;
		if (mid < a + b) l = mid;
		else if (mid > a + b) r = mid;
		else return cout << mid, 0;
	}
	
	return 0;
}

发现 TLE 了,只有 8080 分。我们进行优化:

  • 开了 long long 之后发现 9090 了。
  • 实际上我们无需二分,直接输出答案即可。

真正的正解;

C++
#include<bits/stdc++.h>
using ll = long long;
using namespace std;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int a, b;
	cin >> a >> b;
	cout << a + b;
	return 0;
}
原文