4 Values whose Sum is 0
Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 13069   Accepted: 3669
Case Time Limit: 5000MS

Description

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input

The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .

Output

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

Sample Output

5

Hint

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
题目大意:输入n  表示a b c d 四个集合都有n个元素之后每行输入4个集合中的一个元素,求这四个集合每个集合中拿出一个数 相加等于0的组数。
解题方法:哈希表开放定址法。
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std; #define MAX_VAL 20000000 typedef struct
{
int nCount;
int x;
}Hash; Hash HashTable[MAX_VAL];
int a[], b[], c[], d[]; void InsertHT(int n)
{
int addr = n % MAX_VAL;
if (addr < )
{
addr += MAX_VAL;
}
while(HashTable[addr].nCount != && HashTable[addr].x != n)
{
addr = (addr + ) % MAX_VAL;
}
HashTable[addr].nCount++;
HashTable[addr].x = n;
} int SearchHT(int n)
{
int addr = n % MAX_VAL;
if (addr < )
{
addr += MAX_VAL;
}
while (HashTable[addr].nCount != && HashTable[addr].x != n)
{
addr = (addr + ) % MAX_VAL;
}
return HashTable[addr].nCount;
} int main()
{
int n, ans = ;
scanf("%d", &n);
memset(HashTable, , sizeof(HashTable));
for (int i = ; i < n; i++)
{
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
}
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
InsertHT(a[i] + b[j]);
}
}
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
ans += SearchHT(-c[i] - d[j]);
}
}
printf("%d\n", ans);
return ;
}

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: 25615   Accep ...

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

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

  4. 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 ...

  5. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  6. 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 ...

  7. 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 ...

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

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

  9. POJ 2785 4 Values whose Sum is 0 (二分)题解

    思路: 如果用朴素的方法算O(n^4)超时,这里用折半二分.把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数. 注意和有重复 #include<iost ...

随机推荐

  1. git删除push到远程服务器的commit

    如果不小心把不该提交的代码或者敏感的数据(如密码)提交到远程git服务器上,可以使用git reset回滚到上一个commit,并且commit history不留下任何痕迹. 具体做法: # 1.通 ...

  2. Mac 快捷键整理

    Mac 快捷键整理 文本编辑 适用于文本编辑器,浏览器等 跳到页首 cmd + ↑ 类似windows下的 ctrl + home 跳到页尾 cmd + ↓ 类似windows下的 ctrl + en ...

  3. [游戏模版12] Win32 稳定定时

    >_<:The last time,we learned how to use timer to make the picture run and change show,but some ...

  4. [游戏模版17] Win32 推箱子 迷宫

    >_<:Here introduce a simple game: >_<:resource >_<:only can push a box and finally ...

  5. Qt之Dialog\widget\ mainwindow的区别和布局管理器 & 分裂器的区别

    1.Dialog\widget\ mainwindow的区别 注意mainwindow和widget的区别,mainwindow都工具栏和菜单栏 Dialog and mainwinodws 都是继承 ...

  6. get请求的最大字符长度

    各浏览器HTTP Get请求URL最大长度并不相同,几类常用浏览器最大长度及超过最大长度后提交情况如下: IE6.0                :url最大长度2083个字符,超过最大长度后无法提 ...

  7. java异常处理——题

    1.建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理. public class YiChang { public st ...

  8. spring boot注解之@Scheduled定时任务实现

    java实现定时任务一般使用timer,或者使用quartz组件.现在在spring boot提供了更加方便的实现方式. spring boot已经集成了定时任务.使用@Secheduled注解. @ ...

  9. 写给程序员和UI--Android的切图标准

    最近总是有人在问我,Android怎么切图啊,怎么适配啊,不只是Android同行,还有很多新手ui设计师. 于是我就写篇文章,根据我们平时的开发经验,简单的介绍一下吧. 如果UI设计师以1920*1 ...

  10. 【LeetCode】Power of Two

    问题描写叙述 Given an integer, write a function to determine if it is a power of two. 意:推断一个数是否是2的n次幂 算法思想 ...