**思路:**先枚举两个数然后将两个数的平方和保存起来放入结构体数组中,再枚举另外两个数,利用二分法判断这两个数的平方和是否在刚刚的数组中
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
# include<cmath>
using namespace std;
const int N = 2500010;
struct sum
{
int s,c,d;
bool operator<(const sum& t)const//重载运算符<使得sort可以将结构体的数按照大小排列
{
if(s != t.s)return s < t.s;
if(c != t.c)return c < t.c;
return d < t.d;
}
}sum[N];
int n,m;
int main()
{
cin >> n;
for(int c = 0;c * c <= n;c++)
{
for(int d = c;c * c + d * d <= n;d++)
{
sum[m++] = {c * c + d * d,c,d};
}
}
sort(sum,sum + m);
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;
int l = 0,r = m - 1;
while(l < r)//利用二分判断是否存在一个和t相等的数
{
int mid = (l + r) / 2;
if(sum[mid].s >= t)
{
r = mid;
}
else
{
l = mid + 1;
}
}
if(sum[l].s == t)
{
printf("%d %d %d %d\n",a,b,sum[l].c,sum[l].d);
return 0;
}
}
}
}