Eqs - poj 1840(hash)
题意:对于方程:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ,有xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}. 现在给出a1,a2,a3,a4,a5的值,求出满足上面方程的解有多少个。
思路:hash的应用。暴力枚举的话会达到100^5,明显会超时。所以将方程分成-(a1x13+ a2x23 )和 a3x33+a4x43+ a5x53 两部分,若这两部分相等,则为方程的一个解。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 short hash[25000002]; 5 int main(){ 6 int coff[5],base[101]; 7 int i,j,k; 8 int result=0; 9 for(i=0;i<5;i++){ 10 scanf("%d",&coff[i]); 11 } 12 for(i=-50;i<=50;i++){ 13 int tmp=i*i*i; 14 base[i+50]=tmp; 15 } 16 memset(hash,0,sizeof(hash)); 17 for(i=-50;i<=50;i++){ 18 for(j=-50;j<=50;j++){ 19 20 if(i!=0&&j!=0){ 21 int tmp=coff[0]*base[i+50]+coff[1]*base[j+50]; 22 hash[tmp+12500000]++; 23 } 24 } 25 } 26 27 for(i=-50;i<=50;i++){ 28 for( j=-50;j<=50;j++){ 29 for(k=-50;k<=50;k++){ 30 if(i!=0&&j!=0&&k!=0){ 31 int tmp=coff[2]*base[i+50]+coff[3]*base[j+50]+coff[4]*base[k+50]; 32 tmp=0-tmp; 33 if(tmp<12500000&&tmp>=-12500000) 34 result+=hash[tmp+12500000]; 35 } 36 37 } 38 39 } 40 } 41 printf("%d\n",result); 42 return 0; 43 }
附:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 14021 | Accepted: 6889 |
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.
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