[Swust OJ 166]--方程的解数(hash法)

 

题目链接:http://acm.swust.edu.cn/problem/0166/

Time limit(ms): 5000      Memory limit(kb): 65535
 

有如下方程组: A1*x1^3+A2*x2^3+A3*x3^3+A4*x4^3+A5*x5^3=0,其中A1…A5都在[-50,50]内。 如果(x1,x2,x3,x4,x5)(其中-50<=xi<=50,xi!=0)能让该等式成立,就说(x1,x2,x3,x4,x5)为其一组解,现在的问题是要问你该等式共有多少组解。

Description

输入包括5个系数 A1,A2,A3,A4,A5


注意:这里是多组输入哈,太坑爹了!

Input

输出该方程解的数目

Output
1
23 45 36 13 57
Sample Input
1
1436
 
 
解题思路:明显的hash,但是想了想,这里的系数[-50,50],x范围[-50,50],
     那么求得的解的变换范围为[-12500000,12500000]数组大小25000000(内存直接爆了)
     表示智商捉急~~然后晓得了还有short 这个东东( http://bbs.csdn.net/topics/370037447
     然后卡到内存ac了
代码如下:
 1 #include <stdio.h>

 2 #include <string.h>

 3 #define maxn 25000010

 4 short hash[maxn];

 5 int main(){

 6     int a[6], x1, x2, x3, x4, x5, cnt, temp;

 7     while (scanf("%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5]) != EOF){

 8         cnt = 0;

 9         memset(hash, 0, sizeof(hash));

10         for (x1 = -50; x1 <= 50; x1++){

11             if (x1){

12                 for (x2 = -50; x2 <= 50; x2++){

13                     if (x2){

14                         temp = a[1] * x1*x1*x1 + a[2] * x2*x2*x2;

15                         if (temp<0) 

16                             temp += maxn;

17                         hash[temp]++;

18                     }

19                 }

20             }

21         }

22         for (x3 = -50; x3 <= 50; x3++){

23             if (x3){

24                 for (x4 = -50; x4 <= 50; x4++){

25                     if (x4){

26                         for (x5 = -50; x5 <= 50; x5++){

27                             if (x5){

28                                 temp = -(a[3] * x3*x3*x3 + a[4] * x4*x4*x4 + a[5] * x5*x5*x5);

29                                 if (temp<0) 

30                                     temp += maxn;

31                                 if (hash[temp])

32                                     cnt += hash[temp];

33                             }

34                         }

35                     }

36                 }

37             }

38         }

39         printf("%d\n", cnt);

40     }

41     return 0;

42 }
View Code

 

但是发现有人低内存ac了,然后,然后,把每次求得的值MOD一个数,然后~~~(智商啊)

 1 #include <stdio.h>

 2 #include <math.h>

 3 #include <string.h>

 4 #define maxn 200005

 5 int hash[maxn][10], num[maxn];

 6 int main(){

 7     int i, j, k, l, cnt, tmp, mark, a[5];

 8     while (~scanf("%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4])){

 9         memset(num, 0, sizeof(num));

10         cnt = 0;

11         for (i = -50; i <= 50; i++){

12             if (i){

13                 for (j = -50; j <= 50; j++){

14                     if (j){

15                         tmp = a[0] * i*i*i + a[1] * j*j*j;

16                         mark = abs(tmp) % maxn;

17                         hash[mark][num[mark]] = tmp;

18                         num[mark]++;

19                     }

20                 }

21             }

22         }

23         for (i = -50; i <= 50; i++){

24             if (i){

25                 for (j = -50; j <= 50; j++){

26                     if (j){

27                         for (k = -50; k <= 50; k++){

28                             if (k){

29                                 tmp = a[2] * i*i*i + a[3] * j*j*j + a[4] * k*k*k;

30                                 mark = abs(tmp) % maxn;

31                                 for (l = 0; l < num[mark]; l++)

32                                 if (tmp == hash[mark][l]) cnt++;

33                             }

34                         }

35                     }

36                 }

37             }

38         }

39         printf("%d\n", cnt);

40     }

41     return 0;

42 }
View Code

 

你可能感兴趣的:(hash)