Equations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3625 Accepted Submission(s): 1444
Problem Description
Consider equations having the following form:
a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.
It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.
Determine how many solutions satisfy the given equation.
Input
The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.
End of file.
Output
For each test case, output a single line containing the number of the solutions.
Sample Input
1 2 3 -4
1 1 1 1
Sample Output
39088
0
/*
由a*x1^2 + b*x2^2 + c*x3^2 + d*x4^2 = 0移项得到
a*x1^2 + b*x2^2 = -(c*x3^2 + d*x4^2);考虑到-100<=x<=100,
则可以枚举式子两边可取的值,最后统计结果。统计则用到
hash,将能计算出来的结果作为hash的索引。首先枚举左式,若结果为正,
存到left_hash[],为负则存到right_hash[],其中统计的信息是该结
果出现的次数。枚举右式时,若结果为正,则加上right_hash[],
因为right_hash[]存的是左式为负数时的情况,同理,若右式结果为负
则加上left_hash[]最后结果ans * 16即是答案。因为x1,x2,x3,x4都
是取平方的,每一个都可以取正或者取负,有2^4种情况。
281MS 8084K
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 1000005 //最大索引为100*100*50 + 100*100+50
using namespace std;
int left_hash[SIZE];
int right_hash[SIZE];
int a,b,c,d;
int ans;
int main()
{
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;
}
for(int i=0; i<SIZE; i++)
left_hash[i] = right_hash[i] = 0;
ans = 0;
for(int i=1; i<=100; i++)
{
for(int j=1; j<=100; j++)
{
int temp = a*i*i + b*j*j;
if(temp >= 0)
left_hash[temp] ++;
else
right_hash[-temp] ++;
}
}
for(int i=1; i<=100; i++)
{
for(int j=1; j<=100; j++)
{
int temp = c*i*i + d*j*j;
if(temp > 0)
ans += right_hash[temp];
else
ans += left_hash[-temp];
}
}
printf("%d\n",ans*16);
}
return 0;
}