Equations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1729 Accepted Submission (s): 662
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 blks.
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
Give you the four numbers a, B, c, and d, then ask a * x1 ^ 2 + B * x2 ^ 2 + c * x3 ^ 2 + d * x4 ^ 2 = 0
(X1, x2, x3, x4) How many solutions are there?
When I first read this question, I want to directly find it in four cycles, but this definitely times out, so I used the hash method to solve it. It is very clever! Separate the summation of the two parts. If the sum of the two parts is 0, then add so many, and multiply by 16. This will change from n ^ 4 to 2 * n ^ 2, which is much faster !!!
Code:
Cpp Code
# Include <iostream>
# Include <stdio. h>
# Include <algorithm>
# Include <memory. h>
Using namespace std;
Int f1 [1000005]; // The value is positive.
Int f2 [1000005]; // The number is negative.
Int main ()
{
Int I, j, k, sum;
Int a, B, c, d;
While (scanf ("% d", & a, & B, & c, & d )! = EOF)
{
// Abcd must be greater than 0 or less than 0. You need to add this, or the timeout will not occur.
If (a> 0 & B> 0 & c> 0 & d> 0 | a <0 & B <0 & c <0 & d <0)
{
Printf ("0 \ n ");
Continue;
}
Memset (f1, 0, sizeof (f1 ));
Memset (f2, 0, sizeof (f2 ));
For (I = 1; I <= 100; I ++)
{
For (j = 1; j <= 100; j ++)
{
K = a * I + B * j;
If (k> = 0) f1 [k] ++; // k> = 0 f1 [k] ++
Else f2 [-k] ++; // k <0 f2 [k] ++
}
}
Sum = 0;
For (I = 1; I <= 100; I ++)
{
For (j = 1; j <= 100; j ++)
{
K = c * I + d * j;
If (k> 0) sum + = f2 [k]; // if k is positive, add f2 [k]
Else sum + = f1 [-k]; // if k is negative, add f1 [k]
}
}
Printf ("% d \ n", 16 * sum); // each solution has positive and negative values, and the result has 2 ^ 4 types
}
Return 0;
}