Files
python/TangDou/LuoGuBook/P1147.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

42 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 2000010;
typedef long long LL;
LL s[N];
int main() {
LL n;
cin >> n;
//预处理前缀和
for (int i = 1; i <= n; i++) s[i] = s[i - 1] + i;
// 如果我们预处理出来了s数组,那么两个变量[x,y],通过枚举y --> i 从 1~ n/2
// ∵s[x] - s[y-1] = n
// ∴s[x] = s[y-1] + n
int m = n >> 1; //最少两个数字的和,所以顶天是n/2
for (int i = 1; i <= m; i++) { //枚举起点
LL sx = s[i - 1] + n;
// STL二分找到sx的位置
int pos = lower_bound(s + 1, s + n + 1, sx) - s; // s数长度 n+1
//手写二分找到x的位置
/*
int l = 0, r = n + 1;
while (l < r) {
int mid = (l + r) >> 1;
if (sx <= s[mid])
r = mid;
else
l = mid + 1;
}
int pos = l;
*/
//(1)、二分找到的位置是不小于sx的值,未必就是等于,需要再次检查
//(2)、区间长度就起码是2,如果pos==i表示 10000表示10000就不可以
if (s[pos] - s[i - 1] == n && pos != i)
cout << i << " " << pos << endl;
}
return 0;
}