2024-2-27-1221-四平方和-哈希表版本.cpp

2024-2-27-1221-四平方和-哈希表版本.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include <unordered_map>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
const int N=2500010;
struct sum{
int s,c,d;
bool operator< (const sum &t)const
{
if(s!=t.s)return s<t.s;
if(s!=t.c)return c<t.c;
return d<t.s;
}
}sum[N];
int n,m;
unordered_map<int, PII> S;
int main() //二分版本
{
cin>>n;
for(int c=0;c*c<=n;c++)
for(int d=c;c*c+d*d<=n;d++)
{ int t=c*c+d*d;
if(S.count(t)==0)S[t]={c,d};
}

for(int a=0;a*a<=n;a++)
for(int b=a;b*b+a*a<=n;b++)
{
int t=n-a*a-b*b;
if(S.count(t))
{
printf("%d %d %d %d\n",a,b,S[t].x,S[t].y );
return 0;
}
}
return 0;
}