POJ 2785 4 Values whose Sum is 0(哈希表)
【题目链接】 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(哈希表)的更多相关文章
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [POJ] 2785 4 Values whose Sum is 0(双向搜索)
题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...
随机推荐
- 关于python中的 if __name__ == 'main'
name 是内置变量,它表示的是当前所在模块的名字,同时还能反应一个包的结构. a ├── b │ ├── c.py │ └── __init__.py └── __init__.py 目录中 ...
- 数据库——pymysql模块的使用(13)
1.基本用法——建立链接,获取游标,执行sql语句,关闭 建立远程链接账号和权限 mysql> grant all on *.* to '; Query OK, rows affected, w ...
- jQuery遍历 filter()方法
实例 改变所有 div 的颜色,然后向类名为 "middle" 的类添加边框: $("div").css("background", &qu ...
- 【转】在Unity中读写文件数据:LitJSON快速教程
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 介绍 JSON是一个简单的,但功能强大的序列 ...
- (总结)Linux下查看Nginx Apache MySQL的并发连接数和连接状态
1.查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a ...
- TypeScript类型定义文件(*.d.ts)生成工具
在开发ts时,有时会遇到没有d.ts文件的库,同时在老项目迁移到ts项目时也会遇到一些文件需要自己编写声明文件,但是在需要的声明文件比较多的情况,就需要自动生产声明文件.用过几个库.今天简单记录一下. ...
- BZOJ5301 [Cqoi2018]异或序列 【莫队】
题目链接 BZOJ5301 题解 莫队水题 BZOJ400AC纪念 #include<algorithm> #include<iostream> #include<cst ...
- 【POJ 2728 Desert King】
Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...
- (poj)Sequence Median
Description Given a sequence of N nonnegative integers. Let's define the median of such sequence. If ...
- Linux环境准备20160921
这篇文章,是我准备linux的java环境时候,碰到的各种问题,采用的是centos 6.5版本. 1.卸载open jdk 先查看 rpm -qa | grep java # java-1.4. ...