AcWing 1221. 四平方和
原题链接
简单
作者:
zzoce
,
2021-03-07 15:05:02
,
所有人可见
,
阅读 251
用一个大一点的数组就过了
C++ 代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 5000005;
int n, m;
struct sum
{
int c,d;
bool operator< (const sum& t)const
{
if (c != t.c) return c < t.c;
return d < t.d;
}
}sum[N];
int main()
{
cin >> n;
for (int c = 0; c * c <= n; c++)
for (int d = c; c * c + d * d <= n; d++)
{
if (sum[c * c + d * d].c==sum[c * c + d * d].d&&sum[c * c + d * d].c==0)
{
sum[c * c + d * d] = {c,d};
}
}
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;
if (pow(sum[t].c,2)+pow(sum[t].d,2)==t)
{
printf("%d %d %d %d\n", a, b, sum[t].c, sum[t].d);
return 0;
}
}
}
return 0;
}