【题目链接】 http://poj.org/problem?id=2785

【题目大意】

  给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数

【题解】

  将a+b存入哈希表,反查-c-d

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=4500,mod=1<<22;
int n,a[N],b[N],c[N],d[N],head[mod],cnt;
struct data{int x,nxt,s;}g[mod];
long long ans;
inline int hash(int x){return (x+mod)&(mod-1);}
void insert(int x){
int key=hash(x);
for(int i=head[key];i!=-1;i=g[i].nxt){
if(g[i].x==x){g[i].s++;return;}
}g[cnt].s=1; g[cnt].x=x;
g[cnt].nxt=head[key]; head[key]=cnt++;
}
int search(int x){
int key=hash(x);
for(int i=head[key];i!=-1;i=g[i].nxt){
if(g[i].x==x)return g[i].s;
}return 0;
}
void init(){cnt=0;memset(head,-1,sizeof(head));ans=0;}
int main(){
while(~scanf("%d",&n)){
init();
for(int i=0;i<n;i++)scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for(int i=0;i<n;i++)for(int j=0;j<n;j++)insert(a[i]+b[j]);
for(int i=0;i<n;i++)for(int j=0;j<n;j++)ans+=search(-c[i]-d[j]);
printf("%lld\n",ans);
}return 0;
}

POJ 2785 4 Values whose Sum is 0(哈希表)的更多相关文章

  1. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  2. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  3. POJ - 2785 4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25615   Accep ...

  4. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accep ...

  5. UVa 1152 -4 Values whose Sum is 0—[哈希表实现]

    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...

  6. POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)

    题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...

  7. POJ 2785 4 Values whose Sum is 0 Hash!

    http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...

  8. poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))

    Description The SUM problem can be formulated . In the following, we assume that all lists have the ...

  9. [POJ] 2785 4 Values whose Sum is 0(双向搜索)

    题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...

随机推荐

  1. HashMap 的深入学习

    Java为数据结构中的映射定义了一个接口java.util.Map,此接口主要有四个常用的实现类,分别是HashMap.Hashtable.LinkedHashMap和TreeMap,类继承关系如下图 ...

  2. 团队Alpha版本(七)冲刺

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  3. android ListView与EditText共存错位

    在一个ListView中,如果里面有EditText会很麻烦,因为修改EditText里面的数据会发生错位现象. 这时候,需要在适配器BaseAdapter的getView中设置setTag(),将p ...

  4. IPV4的地址是如何分类的?网络号的范围分别是多少?

    1. A类地址 (1)A类地址第1字节为网络地址,其它3个字节为主机地址. (2)A类地址范围:1.0.0.1—126.255.255.254 (3)A类地址中的私有地址和保留地址: ① 10.X.X ...

  5. GYM - 101147 J.Whistle's New Car

    题意: 给出一颗有点权和边权的树.求每一个点u的子树中有多少点v,使得点v到点u的距离小于等于点v的权值. 题解: 对于每一个点,倍增的预处理出他的祖宗节点及距离.根据预处理的结果求出每个点能到的最远 ...

  6. 【CF Edu 28 A. Curriculum Vitae】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. SQL UNPIVOT和PIVOT

    /* table_source PIVOT( 聚合函数(value_column) FOR pivot_column IN(<column_list>) ) 完整语法: table_sou ...

  8. linux正则表达式(一)

    正则表达式是一种字符串模式匹配,使用灵活.功能强大,使用简单的方式对字符串进行控制. 1.使用grep进行字符串匹配 测试文本 1.txt helloworld haa !@#$%^&*( A ...

  9. 基于类的通用视图(Class-based generic views)

    在web开发中,最令人头痛的就是一遍又一遍的重复固定的模式.在解决了模板层面和模型层面的重复代码之痛之后,Django使用通用视图来解决视图层面的代码重复. 扩展通用视图 毫无疑问通用视图可以大幅度地 ...

  10. 51nod 1851 俄罗斯方块

    玩过俄罗斯方块?那你知道俄罗斯方块共有七种吧(其实只有五种) 给一个黑白图,每次能将某些区域的格子黑白反转,至于某些区域的意思嘛,就是俄罗斯方块形状的区域咯(可水平翻转.上下翻转.旋转) 求能否将图变 ...