题目大意:这个。。就是求满足式子的xi的可能性的数目。
题目链接:http://poj.org/problem?id=1840
方法:hash
思路:我觉得这个题就是hash的入门题,能让你初步理解hash的作用,如果采用最暴力的做法,五重循环。。答案理论上是可以出来的,但是绝对超时,所以我么们可以通过大数组来存储,将等式分为两部分,前三个一部分,后两个一部分,通过记录前部分的值所对应下标的数组赋值1,种数sum在加上后部分的值所对应下标的数组的值,因为两部分的值要相同,所以后部分如果相同,则加上的为1,否则加上的为0(大数组需要先前清0),这里我想说的就是数组开的大小,太大内存超限,太小数组不够存,当然可以取大素数模来减小数组,也可以直接开大数组,不过记得分析。
算法实现:
#include<memory.h>
#include<stdio.h>
#define MAX 25000001
short hash[MAX];
int pow[101];
int main()
{
int a,b,c,d,e;
int i,j,k,sum;
for(i=0;i<101;i++)
pow[i]=(i-50)*(i-50)*(i-50);
while(scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)!=EOF)
{
sum=0;
memset(hash,0,sizeof(hash));
for(i=0;i<101;i++)
for(j=0;j<101;j++)
for(k=0;k<101;k++)
if(i!=50&&j!=50&&k!=50)
if(a*pow[i]+b*pow[j]+c*pow[k]<=MAX/2&&a*pow[i]+b*pow[j]+c*pow[k]>=-MAX/2)
//这是判断是否超限,如果不满足,是不可能满足两部分相等的。 hash[12500000+a*pow[i]+b*pow[j]+c*pow[k]]++;
for(i=0;i<101;i++)
for(j=0;j<101;j++)
if(i!=50&&j!=50)
sum+=hash[12500000-d*pow[i]-e*pow[j]];
printf("%d\n",sum);
}
return 0;
}