【题目链接】 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. 获取访问者ip及其所在城市

    原本使用新浪Api,然后发现不行了,以下小编重新查找了几个,个人推荐太平洋的接口 1.首先获取真实ip $ip = $_SERVER['REMOTE_ADDR']; 2.要知道的Api接口 几个接口$ ...

  2. 孤荷凌寒自学python第四十天python 的线程锁RLock

     孤荷凌寒自学python第四十天python的线程锁RLock (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 因为研究同时在多线程中读写同一个文本文件引发冲突,所以使用Lock锁尝试同步, ...

  3. 团队Alpha(八)冲刺

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

  4. nf_register_hooks解读

    nf_register_hooks是什么 net->netns_nf->nf_hook_entry[NFPROTO_NUMPROTO][NF_MAX_HOOKS=8] (nf)       ...

  5. 【bzoj2396】神奇的矩阵 随机化

    题目描述 给出三个行数和列数均为N的矩阵A.B.C,判断A*B=C是否成立. 输入 题目可能包含若干组数据.对于每组数据,第一行一个数N,接下来给出三个N*N的矩阵,依次为A.B.C三个矩阵. 输出 ...

  6. 架构-UML类图

    在UML 2.0的13种图形中,类图是使用频率最高的UML图之一.Martin Fowler在其著作<UML Distilled: A Brief Guide to the Standard O ...

  7. POJ 1064 Cable master | 二分+精度

    题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...

  8. Web实现数据库链接的登录注册修改密码功能

    /** * Copyright (C), 2017-2017 * FileName: User * Author: ichimoku * Date: 2017/12/5 14:31 * version ...

  9. 关于PDA、GPS等动态资源的几种GIS解决方案

    关于PDA.GPS等动态资源的几种GIS解决方案(原创) 今年来GIS发展迅速,特别是实时监控中引入了GPS,PDA等动态资源,使得GIS在各个行业的应用更为广泛. 1.在这些动态资源资源的监控中主要 ...

  10. 解决hibernate向mysql插入中文乱码问题(更改MySQL字符集)

    1.首先需要修改mysql数据库的配置文件my.ini,此文件放在mysql根目录下.在此文件下查找default-character-set属性,并将其值更改为utf8(注意:不是utf-8,也要注 ...