跳到正文

题解

题解:P1001 A+B Problem

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P1001

版本

  1. v1当前

显然具有单调性,可以二分。

考虑如何二分,很明显下界是 −(2×109)-(2\times 10^9),上界是 2×1092\times 10^9 。

然后考虑 mid,很明显一般的二分都是取中间,但是我们可以取中间的随机数。

然后这个二分跑的飞快 32ms,代码如下:

C++
#include <bits/stdc++.h>
#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1 , 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;

using namespace std;

inline
int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ '0');
		ch = getchar();
	}
	return x * f;
}

const long long N = 4e9;

long long a, b, s, l, r;

mt19937 rd(time(0));

int main() {
	a = read(), b = read();
	l = -2e9, r = 2e9;
	while (l < r) {
		int mid = rd() % (r - l + 1) + l;
		if (mid >= a + b) r = mid;
		else l = mid + 1;
	}
	printf ("%d\n", l);
	return 0;
}

关注 Perfect_Youth 谢谢喵~。

原文