Eqs
Eqs
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15029
题意:
给出系数a1,a2,a3,a4,a5,求出方程a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=0 有多少个解。
样例:
Sample Input
37 29 41 43 47
Sample Output
654
分析:
如果直接用5个for循环是会超时的,所以把方程a1x1x1x1+a2x2x2x2+a3x3x3x3+a4x4x4x4+a5x5x5x5=0
转变成-(a1x1x1x1+a2x2x2x2)=a3x3x3x3+a4x4x4x4+a5x5x5x5这样就将5层循环减少到3层循环。
1 #include<iostream> 2 #include<cstring> 3 #include<cmath> 4 using namespace std; 5 const int maxn=25000000; 6 int i,a[10],count=0,sum=0; 7 short b[maxn]; 8 int x1,x2,x3,x4,x5; 9 int main() 10 { 11 memset(b,0,sizeof(b)); 12 for(i=0;i<5;i++) 13 cin>>a[i]; 14 for(x1=-50;x1<=50;x1++) 15 { if(x1==0) continue; 16 for(x2=-50;x2<=50;x2++) 17 { 18 if(x2==0) continue; 19 sum=-1*(a[0]*x1*x1*x1+a[1]*x2*x2*x2); 20 if(sum<0) 21 b[maxn+sum]++; 22 else b[sum]++; 23 } 24 } 25 for(x3=-50;x3<=50;x3++) 26 { 27 if(x3==0) continue; 28 for(x4=-50;x4<=50;x4++) 29 { 30 if(x4==0) continue; 31 for(x5=-50;x5<=50;x5++) 32 { 33 if(x5==0) continue; 34 sum=a[2]*x3*x3*x3+a[3]*x4*x4*x4+a[4]*x5*x5*x5; 35 if(sum<0) 36 sum=maxn+sum; 37 count=count+b[sum]; 38 } 39 40 } 41 } 42 cout<<count<<endl; 43 return 0; 44 }