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

【题目大意】

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

【题解】

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

【代码】

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. const int N=4500,mod=1<<22;
  6. int n,a[N],b[N],c[N],d[N],head[mod],cnt;
  7. struct data{int x,nxt,s;}g[mod];
  8. long long ans;
  9. inline int hash(int x){return (x+mod)&(mod-1);}
  10. void insert(int x){
  11. int key=hash(x);
  12. for(int i=head[key];i!=-1;i=g[i].nxt){
  13. if(g[i].x==x){g[i].s++;return;}
  14. }g[cnt].s=1; g[cnt].x=x;
  15. g[cnt].nxt=head[key]; head[key]=cnt++;
  16. }
  17. int search(int x){
  18. int key=hash(x);
  19. for(int i=head[key];i!=-1;i=g[i].nxt){
  20. if(g[i].x==x)return g[i].s;
  21. }return 0;
  22. }
  23. void init(){cnt=0;memset(head,-1,sizeof(head));ans=0;}
  24. int main(){
  25. while(~scanf("%d",&n)){
  26. init();
  27. for(int i=0;i<n;i++)scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
  28. for(int i=0;i<n;i++)for(int j=0;j<n;j++)insert(a[i]+b[j]);
  29. for(int i=0;i<n;i++)for(int j=0;j<n;j++)ans+=search(-c[i]-d[j]);
  30. printf("%lld\n",ans);
  31. }return 0;
  32. }

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. 关于python中的 if __name__ == 'main'

    name 是内置变量,它表示的是当前所在模块的名字,同时还能反应一个包的结构. a ├── b │   ├── c.py │   └── __init__.py └── __init__.py 目录中 ...

  2. 数据库——pymysql模块的使用(13)

    1.基本用法——建立链接,获取游标,执行sql语句,关闭 建立远程链接账号和权限 mysql> grant all on *.* to '; Query OK, rows affected, w ...

  3. jQuery遍历 filter()方法

    实例 改变所有 div 的颜色,然后向类名为 "middle" 的类添加边框: $("div").css("background", &qu ...

  4. 【转】在Unity中读写文件数据:LitJSON快速教程

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 介绍 JSON是一个简单的,但功能强大的序列 ...

  5. (总结)Linux下查看Nginx Apache MySQL的并发连接数和连接状态

    1.查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a ...

  6. TypeScript类型定义文件(*.d.ts)生成工具

    在开发ts时,有时会遇到没有d.ts文件的库,同时在老项目迁移到ts项目时也会遇到一些文件需要自己编写声明文件,但是在需要的声明文件比较多的情况,就需要自动生产声明文件.用过几个库.今天简单记录一下. ...

  7. BZOJ5301 [Cqoi2018]异或序列 【莫队】

    题目链接 BZOJ5301 题解 莫队水题 BZOJ400AC纪念 #include<algorithm> #include<iostream> #include<cst ...

  8. 【POJ 2728 Desert King】

    Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...

  9. (poj)Sequence Median

    Description Given a sequence of N nonnegative integers. Let's define the median of such sequence. If ...

  10. Linux环境准备20160921

    这篇文章,是我准备linux的java环境时候,碰到的各种问题,采用的是centos 6.5版本. 1.卸载open jdk  先查看 rpm -qa | grep java  # java-1.4. ...