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层循环。
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=;
int i,a[],count=,sum=;
short b[maxn];
int x1,x2,x3,x4,x5;
int main()
{
memset(b,,sizeof(b));
for(i=;i<;i++)
cin>>a[i];
for(x1=-;x1<=;x1++)
{ if(x1==) continue;
for(x2=-;x2<=;x2++)
{
if(x2==) continue;
sum=-*(a[]*x1*x1*x1+a[]*x2*x2*x2);
if(sum<)
b[maxn+sum]++;
else b[sum]++;
}
}
for(x3=-;x3<=;x3++)
{
if(x3==) continue;
for(x4=-;x4<=;x4++)
{
if(x4==) continue;
for(x5=-;x5<=;x5++)
{
if(x5==) continue;
sum=a[]*x3*x3*x3+a[]*x4*x4*x4+a[]*x5*x5*x5;
if(sum<)
sum=maxn+sum;
count=count+b[sum];
} }
}
cout<<count<<endl;
return ;
}