题目大意:已知方程a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ,其中ai的范围在【-50,50】中,xi的范围在【-50,50】中且xi都不为0,(i=1,2,3,4,5)。求在给定a1,a2,a3,a4,a5的值,有多少种使得方程成立的情况即x1,x2,x3,x4,x5有多少种可能的取值。
本题思路:大部分的人看到题后,都会直接枚举x1,x2,x3,x4,x5,但枚举五个变量有100的5次方次,肯定会超时。
所以需要用到方程变形 a3x33+ a4x43+ a5x53 = -a1x13_ a2x23
先枚举x1,x2,把结果保存在hash表中,然后再枚举x3,x4,x5,在哈希表中找结果,如果满足上述的方程就记录下来。
#include<stdio.h>
#include<string.h>
const int maxn=6;
const int MP=1250319;
struct node
{
int v;
node* next;
};
int x[maxn];
int a[maxn];
node* pt;
node* hash[MP+1];
int ans;
int main()
{
int t,p;
memset(hash,0,sizeof(hash));
for(int i=0; i<5; i++)
scanf("%d",&a[i]);
for(x[0]=-50; x[0]<=50; x[0]++)
{
if(x[0])
{
for(x[1]=-50; x[1]<=50; x[1]++)
{
if(x[1])
{
t=x[0]*x[0]*x[0]*a[0]+x[1]*x[1]*x[1]*a[1];
p=(t>0? t:-t)%MP;
pt=new node;
pt->v=t;
pt->next=hash[p];
hash[p]=pt;
}
}
}
}
ans=0;
for(x[2]=-50; x[2]<=50; x[2]++)
if(x[2])
for(x[3]=-50; x[3]<=50; x[3]++)
if(x[3])
for(x[4]=-50; x[4]<=50; x[4]++)
if(x[4])
{
t=x[2]*x[2]*x[2]*a[2]+x[3]*x[3]*x[3]*a[3]+x[4]*x[4]*x[4]*a[4];
p=(t>0?t:-t)%MP;
pt=hash[p];
while(pt)
{
if(pt->v==t)
ans++;
pt=pt->next;
}
}
printf("%d\n",ans);
return 0;
}