题目链接:http://poj.org/problem?id=1840
题解:
首先可以想到最简单的五层循环暴力枚举,肯定会超时
我们可以将等式转化一下
a1x31+a2x32+a3x33+a4x34+a5x35=0
等价于
a1x31+a2x32=−(a3x33+a4x34+a5x35)
所以只需要分别枚举
x1,x2,x3和x4,x5
,判断是否是相反数即可
考虑到计算出来的值可能比较大,所以将数字哈希一下,用map即可实现
复杂度
O(1003+1002)
,非常科学
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int a[6];
map<int,int> hash;
int main()
{
for(int i=1;i<=5;i++) cin>>a[i];
for(int i=-50;i<=50;i++)
for(int j=-50;j<=50;j++)
{
if(i==0||j==0) continue;
int tmp=i*i*i*a[1]+j*j*j*a[2];
if(hash.find(tmp)!=hash.end()) hash[tmp]++;
else hash[tmp]=1;
}
long long 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 tmp=-(i*i*i*a[3]+j*j*j*a[4]+k*k*k*a[5]);
if(hash.find(tmp)==hash.end()) continue;
ans+=hash[tmp];
}
printf("%lld\n",ans);
return 0;
}