POJ 1840 Eqs(哈希表)

本文介绍了一种解决特定数学问题的算法实现。通过构造哈希表存储多项式的计算结果,利用三次方运算,寻找符合条件的四元组,进而统计解的数量。该算法使用C++实现,涉及哈希表、链表等数据结构。

题目链接

模版题。

 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 }

 

转载于:https://www.cnblogs.com/naix-x/archive/2013/01/15/2860720.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值