Consider equations having the following form:
a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=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
大致意思就是一个五元三次方程组,求解的个数,五个变量有范围限制-50~50且不等于0
先移项,把x1和x2移到另一边边个号(a3x3^3+ a4x4^3+ a5x5^3) =-(a1x1^3+ a2*x2^3)
#include <iostream>
#include <stdio.h>
#include <set>
#include <cmath>
#include <algorithm>
#define MAX 25000000
using namespace std;
short has[MAX+10];//这里只能用short否则会内存超限
int main()
{
int a1,a2,a3,a4,a5,sum;
int con=0;
scanf("%d %d %d %d %d",&a1,&a2,&a3,&a4,&a5);
for(int x1=-50;x1<=50;x1++)
{
if(x1==0)
continue;
for(int x2=-50;x2<=50;x2++)
{
if(x2==0)
continue;
sum=-1*x1*x1*x1*a1+x2*x2*x2*a2;
if(sum<0)
sum+=MAX;//数组下标不能为负数,所以加上一个大最数把下标转化一下
has[sum]++;//每次左边出现一次sum就记一次数
}
}
for(int x3=-50;x3<=50;x3++)
{
if(x3==0)
continue;
for(int x4=-50;x4<=50;x4++)
{
if(x4==0)
continue;
for(int x5=-50;x5<=50;x5++)
{
if(x5==0)
continue;
sum=x3*x3*x3*a3+x4*x4*x4*a4+x5*x5*x5*a5;
if(sum<0)
sum+=MAX;
if(has[sum])//当左边存在sum时 计数
con+=has[sum];
}
}
}
printf("%d\n",con);
return 0;
}