http://acm.hdu.edu.cn/showproblem.php?pid=1496
给定a,b,c,d。a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
其中x1~x4 在[-100,100]区间内,a,b,c,d在[-50,50] 区间内。
求满足上面那个式子的所有解的个数。
思路:
这题用hash的思想很巧妙,先对x1和x2进行枚举,存在的存进hash表中,然后接下来枚举x3和x4,如果恰好和前面的为相反数,那么答案+1.
#include<cstdio>
#include<cstring>
const int MAXN=50*100*100*2+10;
int hash_pos[MAXN]; //positive
int hash_neg[MAXN]; //negative
int res[101];
int main()
{
int a,b,c,d ;
for(int i=1;i<=100;i++)
res[i]=i*i;
while(~scanf("%d%d%d%d",&a,&b,&c,&d))
{
if(a>0 && b>0 && c>0 && d>0||a<0 && b<0 && c<0 && d<0)
{
printf("0\n");
continue;
}
memset(hash_pos,0,sizeof(hash_pos));
memset(hash_neg,0,sizeof(hash_neg));
for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
int x=res[i]*a+res[j]*b;
if(x >=0)
hash_pos[x]++;
else
hash_neg[-x]++;
}
}
int cnt=0;
for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
int x=res[i]*c+res[j]*d;
if(x >0)
cnt+=hash_neg[x];
else
cnt+=hash_pos[-x];
}
}
printf("%d\n",cnt<<4);
}
return 0;
}