您的位置:首页 > 其它

Eqs - poj 1840(hash)

2015-08-10 11:02 519 查看
题意:对于方程: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 两部分,若这两部分相等,则为方程的一个解。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
short hash[25000002];
int main(){
int coff[5],base[101];
int i,j,k;
int result=0;
for(i=0;i<5;i++){
scanf("%d",&coff[i]);
}
for(i=-50;i<=50;i++){
int tmp=i*i*i;
base[i+50]=tmp;
}
memset(hash,0,sizeof(hash));
for(i=-50;i<=50;i++){
for(j=-50;j<=50;j++){

if(i!=0&&j!=0){
int tmp=coff[0]*base[i+50]+coff[1]*base[j+50];
hash[tmp+12500000]++;
}
}
}

for(i=-50;i<=50;i++){
for( j=-50;j<=50;j++){
for(k=-50;k<=50;k++){
if(i!=0&&j!=0&&k!=0){
int tmp=coff[2]*base[i+50]+coff[3]*base[j+50]+coff[4]*base[k+50];
tmp=0-tmp;
if(tmp<12500000&&tmp>=-12500000)
result+=hash[tmp+12500000];
}

}

}
}
printf("%d\n",result);
return 0;
}


附:

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 14021Accepted: 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.
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: