二分+哈希:
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 5e6 + 10;
int h[N];
int n, m = 0;
struct node
{
int sum, c, d;
bool operator < (const node &a) const{
if (sum == a.sum)
{
if (c == a.c)
return d < a.d;
return c < a.c;
}
return sum < a.sum;
}
}q[N];
int main ()
{
ios::sync_with_stdio (false);
cin.tie (0); cout.tie(0);
cin >> n;
for (int i = 0; i * i <= n; i ++)
for (int j = i; j * j + i * i <= n; j ++)
q[m ++] = {i * i + j * j, i, j};
sort (q, q + m);
for (int i = 0; i * i <= n; i ++)
for (int j = i; j * j + i * i <= n; j ++)
{
int t = n - i * i - j * j;
int l = 0, r = m - 1;
while (l < r)
{
int mid = (l + r) >> 1;
if (t <= q[mid].sum) r = mid;
else l = mid + 1;
}
if (q[l].sum == t)
{
cout << i << " " << j << " " << q[l].c << " " << q[l].d;
return 0;
}
}
}