链接:http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=11900#problem/B
B - Eqs
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uSubmit Status Practice POJ 1840
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
题意:就是五元三次方程的根的组数
思路:一看到题目,就想到用最朴素的算法计算,五层循环,一一枚举,可是肯定超时!!!!
于是想从hash方面考虑,可是一想到有负值的情况就立马否决了于是今天又0AC了
看到希望的曙光就千万别放弃,一切都会迎刃而解的,只要肯思考,!!!!!!!!!
事实证明这就是用hash做的,要点有三:1如何处理下标为负的情况
2.分成两组循环,3,利用下标使等式无形中成立
#include<stdio.h>//分步计算 #include<string.h> #include<iostream> using namespace std; const int maxn=25000000;//maxn为特殊情况下的最大值 char h[maxn];// 这里必须设为字符型,否则超内存 int main() { int i,j,k,s,t; int a1,a2,a3,a4,a5; while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)!=EOF) { s=0; memset(h,0,sizeof(h)); for(i=-50;i<=50;i++) for(j=-50;j<=50;j++) if(i!=0&&j!=0) { t=a1*i*i*i+a2*j*j*j; h[t+maxn/2]++;} //这样的目的是既标记了情况又防止下标为负值 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) {t=a3*i*i*i+a4*j*j*j+a5*k*k*k; if(t>=-maxn/2&&t<=maxn/2)//针对上面循环的出的情况下的数 s+=h[-t+maxn/2];//使等式成立的情况 } printf("%d\n",s); } return 0; }