AcWing 1221. 四平方和
原题链接
简单
作者:
RwChen
,
2021-06-29 00:23:07
,
所有人可见
,
阅读 402
打表也太快了吧,学到了
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 5e6 + 10;
int h[maxn];
int main()
{
int n;
cin >> n;
for(int i = 0; i*i < n; i++)
for(int j = i; j*j <= n - i*i; j++)
{
if(!h[i*i + j*j])
h[i*i + j*j] = j;
}
if(!n)
{
cout << "0 0 0 0" << endl;
return 0;
}
for(int a = 0; a*a <= n; a++)
{
for(int b = a; b*b <= n - a*a; b++)
{
int idx = n - a*a - b*b;
if(h[idx])
{
int d = h[idx];
int c = sqrt(idx - d * d);
cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
return 0;
}
}
}
return 0;
}