Time Limit: 3000 MS Memory Limit: 32768 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
[Submit] [Status]
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.
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.
For each test case, output a single line containing the number of the solutions.
1 2 3 -4
1 1 1 1
39088
0
给定a,b,c,d让求满足a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4=0的解的个数 xi != 0。
n^4暴力肯定超时,可以把方程分成两个部分,t=a*x1*x1+b*x2*x2所有可能的答案遍历一遍,如果t>=0,就将zh[t]+1,否则将fu[-t]+1,再把t=c*x3*x3+d*x4*x4的值遍历一遍,若t>0,就看fu[t]中的解的个数,否则看zh[-t]。最后将结果乘16,因为每一个x都有一个正数一个负数可取,即2^4个。
#include
#include
#include
#define LL long long
using namespace std;
int fu[1000005], zh[1000005];
int main()
{
int a, b, c, d;
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)) ///若a,b,c,d同号,则一定无解
printf("0\n");
else{
memset(fu, 0, sizeof(fu));
memset(zh, 0, sizeof(zh));
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) zh[temp] ++; ///记录正
else fu[-temp] ++; ///记录负
}
}
int ans = 0;
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 += fu[temp]; ///查找正
else ans += zh[-temp]; ///查找负
}
}
printf("%d\n", ans*16); ///结果有2^4=16个
}
}
}