You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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