结构体重载符号固定格式
bool operator< (const struct &a)const
{
return c<a.c
}
#include<bits/stdc++.h>
using namespace std;
const int N = 1e7;
int n,m;
struct Sum
{
int s,c,d;
bool operator< (const Sum &a)const
{
if(s != a.s)return s<a.s;
if(c != a.c)return c<a.c;
return d<a.d;
}
}sum[N];
int main()
{
cin>>n;
for(int c = 0;c*c<=n;c++)
for(int d = c;d*d+c*c<=n;d++)//如果没有c*c+d*d,只考虑d*d可能会导致d不合法
sum[m++] = {c*c+d*d,c,d};
sort(sum,sum+m);
for(int a = 0;a*a<=n;a++)
for(int b = 0 ; a*a+b*b<=n;b++)
{
int t = n-a*a-b*b;
int l = 0,r = m-1; //枚举的是sum的s sum的大小为m
while(l<r)
{
int mid = (l+r)>>1;
if(sum[mid].s >= t) r = mid;
else l = mid+1;
}
if(sum[l].s ==t)
{
printf("%d %d %d %d",a,b,sum[l].c,sum[l].d);
return 0;
}
}
return 0;
}