题解:P1001 A+B Problem

· · 题解

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

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

#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 了,只有 80 分。我们进行优化:

真正的正解;

#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;
}