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. [游戏模版21] Win32 物理引擎 能量守恒

    >_<:Only a little change in the function of MyPaint(...),besides the initial value have some c ...

  2. Spring - 初始化spring容器

    2016.01.12 学习linux内核的过程中发现变相的提升了自己的工程能力.以前觉得spring这些东西很复杂麻烦.然而,学了linux内核再看这些东西,发现好简单. 学习spring首先就要学习 ...

  3. 深入理解 CSS 的 :before 和 :after 选择器(制作select下拉列表美化插件)

    原文链接:http://www.cnblogs.com/LY-leo/p/5765598.html 对于 :before 和 :after 选择器,大家并不陌生,但是很少有人会主动去用它们.先解释下它 ...

  4. Leetcode 328 Odd Even Linked List 链表

    Given 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL. 就是将序号为单数的放在前面,而 ...

  5. asynchttpClient框架关于多文件批量上传的问题,改用xUtil

    RequestParams params = new RequestParams(); params.add("ordernum",ordernum); params.add(&q ...

  6. Puppet Openstack Mitaka Design Summit小结

    Puppet Openstack Design Summit小结 经过Puppet Openstack社区的不断努力,Puppet Openstack社区目前提供的Official Modules已经 ...

  7. quartzScheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak解决

    01-Jul-2016 07:24:20.218 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 80 ...

  8. Remove WebCakeDesktop

    WebCakeDesktop.Updater.exe 是广告程序,卸载步骤参考 http://malwaretips.com/blogs/webcake-desktop-updater-exe-rem ...

  9. What is a Statistic?

    from: https://controls.engin.umich.edu/wiki/index.php/Basic_statistics:_mean,_median,_average,_stand ...

  10. [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...