题意:
给a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
首先移项,-(a1x13+ a2x23)= a3x33+ a4x43+ a5x53,这样原本的五层循环便最多只用三层循环了,思路是先把所有左边的sum值存下来,然后计算右边的,如果右边有和左边相等的值,则该组数据满足条件
x的范围是-50到50,x的3次方最大为50*50*50=125000,故范围是-125000-125000,开一个hash数组,下标表示sum值,hash[i]对应的值只是计数。
另外把负值要弄成正的存。
这题直接看了前辈题解orz,,,感觉对hash函数有了那么一点概念了,应该怎么构造hash函数呢,理论网上有很多,实际情况做题了再说吧~
#include<iostream>
using namespace std;
short hash[25000001];
int main(void)
{
int a1,a2,a3,a4,a5; //系数
while(cin>>a1>>a2>>a3>>a4>>a5)
{
memset(hash,0,sizeof(hash));
for(int x1=-50;x1<=50;x1++)
{
if(!x1)
continue;
for(int x2=-50;x2<=50;x2++)
{
if(!x2)
continue;
int sum=(a1*x1*x1*x1 + a2*x2*x2*x2)*(-1);
if(sum<0)
sum+=25000000;
hash[sum]++;
}
}
int solution=0;
for(int x3=-50;x3<=50;x3++)
{
if(!x3)
continue;
for(int x4=-50;x4<=50;x4++)
{
if(!x4)
continue;
for(int x5=-50;x5<=50;x5++)
{
if(!x5)
continue;
int sum=a3*x3*x3*x3 + a4*x4*x4*x4 + a5*x5*x5*x5;
if(sum<0)
sum+=25000000;
if(hash[sum])
solution+=hash[sum];
}
}
}
cout<<solution<<endl;
}
return 0;
}