http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1013
Eqs
Time Limit: 5000 MS Memory Limit: 65536 K
Total Submit: 566(229 users) Total Accepted: 330(212 users) Rating: Special Judge: No
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
For each test case :
Line 1: Five coefficients a1, a2, a3, a4, a5, separated by blanks.
Process to the end of file.
Output
For each test case :
Line 1: A single integer that is the number of the solutions for the given equation.(The output will fit in 32 bit signed integers.)
Sample Input
37 29 41 43 47
Sample Output
654
本题的做法就是把时间复杂度转化为空间复杂度,就是把五层for给肢解成一个2层for和一个三层for,加一个空间复杂度很高的数组,然后这个题就非常完美的解决掉了。
下面是AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char num[20000005];
int main()
{
int a1,a2,a3,a4,a5;
while(~scanf("%d %d %d %d %d",&a1,&a2,&a3,&a4,&a5))
{
int num1;
memset(num,0,sizeof(num));
for(int i=-50;i<=50;i++)
{
if(i==0)continue;
for(int j=-50;j<=50;j++)
{
if(j==0)continue;
num1=a1*i*i*i+a2*j*j*j;
if(num1<0)
{
num1+=20000000;//此处仔细想想
}
num[num1]++;
}
}
int sum=0;
for(int i=-50;i<=50;i++)
{
if(i==0)continue;
for(int j=-50;j<=50;j++)
{
if(j==0)continue;
for(int k=-50;k<=50;k++)
{
if(k==0)continue;
num1=a3*i*i*i+a4*j*j*j+a5*k*k*k;
if(num1<0)num1+=20000000;
if(num[num1])
sum+=num[num1];
}
}
}
printf("%d\n",sum);
}
return 0;
}