POJ 1840 Eqs(哈希表)

题目链接

模版题。

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstdlib>

 4 #include <cstring>

 5 #include <cmath>

 6 #include <algorithm>

 7 #include <set>

 8 #include <map>

 9 #include <vector>

10 #include <queue>

11 #include <ctime>

12 using namespace std;

13 #define MOD 97777

14 #define MAXN 1000000

15 struct node

16 {

17     int data;

18     struct node *next;

19 }*head[MOD],hash[MAXN];

20 int cube(int x)

21 {

22     return x*x*x;

23 }

24 int main()

25 {

26     int i,j,k,a1,a2,a3,a4,a5,key,m,num = 0,ans = 0;

27     scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);

28     for(i = -50;i <= 50;i ++)

29     {

30         if(i == 0) continue;

31         for(j = -50;j <= 50;j ++)

32         {

33             if(j == 0) continue;

34             key = a1*cube(i) + a2*cube(j);

35             node *p;

36             p = &hash[num++];

37             p -> data = key;

38             key = key%MOD;

39             if(key < 0)

40             key += MOD;

41             p -> next = head[key];

42             head[key] = p;

43         }

44     }

45     for(i = -50;i <= 50;i ++)

46     {

47         if(i == 0) continue;

48         for(j = -50;j <= 50;j ++)

49         {

50             if(j == 0) continue;

51             for(k = -50;k <= 50;k ++)

52             {

53                 if(k == 0) continue;

54                 key = a3*cube(i) + a4*cube(j) + a5*cube(k);

55                 m = (-key)%MOD;

56                 if(m < 0) m += MOD;

57                 node *p;

58                 for(p = head[m];p != NULL;p = p->next)

59                 {

60                     if(p->data == -key)

61                     ans ++;

62                 }

63             }

64         }

65     }

66     printf("%d\n",ans);

67     return 0;

68 }

 

你可能感兴趣的:(poj)