原方程变换为:
(a1*x1^3+a2*x2^3) = - (a3*x3^3+a4*x4^3+a5*x5^3)
左部枚举出值打表,右部枚举出值的相反数在表中若已经存在,则sum加上已经存在的个数。
如暴力枚举则必然TLE,O(n^5),变换后降维,复杂度为O(n^2+n^3)。
code:
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
int a[6];
map<int, int> h;
int main()
{
//fstream in("input.txt");
for (int i = 1; i < 6; i++)
cin >> a[i];
for (int i = -50; i <= 50; i++)
{
for (int j = -50; j <= 50; j++)
{
if (i == 0 || j == 0)//xi!=0
continue;
int key = i * i * i * a[1] + j * j * j * a[2];
if (h.find(key) == h.end())//if not have , set value=1
h[key] = 1;
else
h[key]++;
}
}
int ans = 0;
for (int i = -50; i <= 50; i++)
{
for (int j = -50; j <= 50; j++)
{
for (int k = -50; k <= 50; k++)
{
if (i == 0 || j == 0 || k == 0)
continue;
int key = i * i * i * a[3] + j * j * j * a[4] + k * k * k * a[5];
if (h.find(0 - key) == h.end())
continue;
ans += h[0 - key];
}
}
}
cout << ans << endl;
//system("pause");
return 0;
}