POJ-1840 Eqs 解题报告

来源:互联网 发布:java流程设计 编辑:程序博客网 时间:2024/04/30 03:03

Description

Consider equations having the following form:
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.

Determine how many solutions satisfy the given equation.

Input

The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.

Output

The output will contain on the first line the number of the solutions for the given equation.

Sample Input

37 29 41 43 47

Sample Output

654
题目大意:这个。。就是求满足式子的xi的可能性的数目。
题目链接:http://poj.org/problem?id=1840
方法:hash
思路:我觉得这个题就是hash的入门题,能让你初步理解hash的作用,如果采用最暴力的做法,五重循环。。答案理论上是可以出来的,但是绝对超时,所以我么们可以通过大数组来存储,将等式分为两部分,前三个一部分,后两个一部分,通过记录前部分的值所对应下标的数组赋值1,种数sum在加上后部分的值所对应下标的数组的值,因为两部分的值要相同,所以后部分如果相同,则加上的为1,否则加上的为0(大数组需要先前清0),这里我想说的就是数组开的大小,太大内存超限,太小数组不够存,当然可以取大素数模来减小数组,也可以直接开大数组,不过记得分析。
算法实现:
#include<memory.h>#include<stdio.h>#define MAX 25000001short 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;}

原创粉丝点击