首先,五层遍历肯定是必死的…
将左右等式分开计算,则时间复杂度降到o(n^3)。
具体的看代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll tri(ll n) { return n * n * n; }
short nums[20000000];
int main()
{
ll res = 0;
int a, b, c, d, e;
while (cin >> a >> b >> c >> d >> e)
{
for (int x1 = -50; x1 <= 50; x1++)
{
if (x1 == 0)
continue;
for (int x2 = -50; x2 <= 50; x2++)
{
if (x2 == 0)
continue;
for (int x3 = -50; x3 <= 50; x3++)
{
if (x3 == 0)
continue;
ll temp = a * tri(x1) + b * tri(x2) + c * tri(x3);
if (temp > 0)
nums[temp]++;
}
}
}
for (int x4 = -50; x4 <= 50; x4++)
{
if (x4 == 0)
continue;
for (int x5 = -50; x5 <= 50; x5++)
{
if (x5 == 0)
continue;
ll temp = d * tri(x4) + e * tri(x5);
if (temp > 0)
res += nums[temp];
}
}
cout << res * 2 << endl;
}
return 0;
}