Fork me on GitHub

HDU ACM 1496 Equations

Equations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3978    Accepted Submission(s): 1602


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
 
Author
LL
 
Recommend
LL
 
解题思路:暴力+hash,主要是想练练hash,一直认为hash是高端黑,用线性探测再散列处理冲突,记得不知道谁说过处理余数的数一般用上素数,不要忘了最后乘上16,因为四个数有可能是正负
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MAXN 50001
 4 int num[MAXN], store[MAXN];
 5 
 6 int hash(int cur)
 7 {
 8     int temp = cur%MAXN;
 9     if(temp < 0) temp += MAXN;
10     while(num[temp] != 0 && store[temp] != cur)
11     {
12         temp = (temp+1)%MAXN;
13     }
14     return temp;
15 }
16 
17 int main()
18 {
19 //    freopen("input.txt", "r", stdin);
20     int rate[101], a, b, c, d, i, j, sum, temp, res;
21     for(i=0; i<101; ++i) rate[i] = i*i;
22     while(scanf("%d%d%d%d", &a, &b, &c, &d) != EOF)
23     {
24         if((a>0&&b>0&&c>0&&d>0) || (a<0&&b<0&&c<0&&d<0))
25         {
26             printf("0\n");
27             continue;
28         }
29         memset(num, 0, sizeof(num));
30         for(i=1; i<101; ++i)
31             for(j=1; j<101; ++j)
32             {
33                 temp = a*rate[i]+b*rate[j];
34                 res = hash(temp);
35                 store[res] = temp;
36                 num[res]++;
37             }
38             
39             sum = 0;
40             
41         for(i=1; i<101; ++i)
42             for(j=1; j<101; ++j)
43             {
44                 temp = -(c*rate[i]+d*rate[j]);
45                 res = hash(temp);
46                 sum += num[res];
47             }
48             
49         printf("%d\n", sum*2*2*2*2);
50     }
51     return 0;
52 }

 

posted @ 2013-07-01 15:25  Gifur  阅读(591)  评论(0编辑  收藏  举报
TOP