Files
python/TangDou/CSP-J/J2-2021/AcWing1221_ErFen_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

54 lines
1.4 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 = 5e6 + 10;
struct Node {
int c, d, sum;
bool operator<(const Node &t) const {
if (sum != t.sum) return sum < t.sum;
if (c != t.c) return c < t.c;
return d < t.d;
}
} f[N];
int n;
int idx;
int main() {
scanf("%d", &n);
// C预处理出 c^2+d^2
for (int c = 0; c * c <= n; c++)
for (int d = c; c * c + d * d <= n; d++)
f[idx++] = {c, d, c * c + d * d};
//结构体排序
sort(f, f + idx);
//枚举a^2+b^2
for (int a = 0; a * a <= n; a++) {
for (int b = a; a * a + b * b <= n; b++) {
int t = n - a * a - b * b;
//手写二分模板,左闭右开
// STL二分缺点:
// 1、常数较大,速度慢
// 2、对于结构体二分,需要构造空的Struct,还需要有一些玄学的赋零操作,不推荐
//结论:全面采用手写二分办法,忘记STL的二分写法
int l = 0, r = idx;
while (l < r) {
int mid = (l + r) >> 1;
if (f[mid].sum >= t)
r = mid;
else
l = mid + 1;
}
if (f[l].sum == t) {
cout << a << ' ' << b << ' ' << f[l].c << ' ' << f[l].d << ' ' << endl;
exit(0);
}
}
}
return 0;
}