4 Values whose Sum is 0

Time Limit: 15000MS Memory Limit: 228000K

Total Submissions: 17875 Accepted: 5255

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

Source

Southwestern Europe 2005

哈希的一道比较简单的题,

题意:给你四个集合,从四个集合中分别选出一个元素,使四个元素的和为零,问有几种选法;

方法:先让两个集合加和,用哈希链表,储存计算的结果,有后两个集合的计算结果查找,不过直接写链表可能比较耗时,我交了一次11s多,后来换成前向星3s多.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout) const int MAX = 4010; const int Mod = 10000007;
struct node
{
int num;
int data;
int next;
} H[Mod*10];
int head[Mod];
int a[MAX],b[MAX],c[MAX],d[MAX];
int top;
int Creat(int ans)
{
H[top].num=1;
H[top].data=ans;
return top++;
}
int main()
{
int n;
int ans;
int p,q;
int Max;
while(~scanf("%d",&n))
{
memset(head,-1,sizeof(head));
for(int i=0; i<n; i++)
{
scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
}
top=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
ans=a[i]+b[j];
p=abs(ans)%Mod;
q=head[p];
while(q!=-1)
{
if(H[q].data==ans)
{
H[q].num++;
break;
}
q=H[q].next;
}
if(q==-1)
{
q=Creat(ans);
H[q].next=head[p];
head[p]=q;
}
}
}
Max=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
ans=-1*(c[i]+d[j]);
p=abs(ans)%Mod;
q=head[p];
while(q!=-1)
{
if(H[q].data==ans)
{
Max+=H[q].num;
break;
}
q=H[q].next;
}
}
}
printf("%d\n",Max);
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏的更多相关文章

  1. Max Sum—hdu1003(简单DP) 标签: dp 2016-05-05 20:51 92人阅读 评论(0)

    Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. winform只允许一个应用程序运行 2014-12-08 09:51 31人阅读 评论(0) 收藏

    使用互斥体Mutex类型 导入命名空间 using System.Threading; //声明互斥体 Mutex mutex = new Mutex(false, "ThisShouldO ...

  3. Java内部类总结 分类: 原理 2015-06-28 09:51 9人阅读 评论(0) 收藏

    内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的一个成员,并且依附于外部类而存在的. 内部类可为静态,可用protected和private修饰(而外部类只能使用public和缺省的包访问 ...

  4. windows设置多长时间后自动关机 分类: windows常用小技巧 2014-04-15 09:35 230人阅读 评论(0) 收藏

    分二步: 第一步:点击windows键,在"搜索程序和文件"的文本框输入:cmd 第二步:输入:shutdown -s -t          (设置电脑一小时后自动关机) 备注: ...

  5. 快速查询本机IP 分类: windows常用小技巧 2014-04-15 09:28 138人阅读 评论(0) 收藏

    第一步: 点击windows建(屏幕左下方),在搜索程序和文件文本框内输入:cmd 第二步:      点击Enter建进入. 第三步: 输入:ipconfig即可. 版权声明:本文为博主原创文章,未 ...

  6. sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏

    sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...

  7. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  8. Ombrophobic Bovines 分类: POJ 图论 最短路 查找 2015-08-10 20:32 2人阅读 评论(0) 收藏

    Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16539 Accepted: 3605 ...

  9. PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏

    PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...

随机推荐

  1. nginx:403 forbidden 二种原因

    出现403 forbidden的两种原因:1.是缺少索引文件(index.html/inde.php):2.是权限问题 一.缺少索引文件index.html/inde.php 比如下面的配置: ser ...

  2. SqlServer:传递超长字符串参数时,参数被自动换行。

    declare @ids nvarchar(max); set @ids=N'5936593066,5936556893,59366 00375,5936594808,59 36624757,5936 ...

  3. Java数据库操作大全

    1.提取单条记录 //import java.sql.*; Connection con=null; Statement stmt=null; ResultSet %%6=null; try { Cl ...

  4. Ruby操作Excel的方法与技巧大全

    测试工作中,批量的数据通常会放到excel表格中,测试输出的数据写回表格中,这样输入输出易于管理,同时清晰明了 使用ruby来操作excel文件首先需要在脚本里包含以下语句 require'win32 ...

  5. fork子进程之间传输信息的方法(包含子进程与子进程之间区分的问题的解决)

    今天看到一道题,要求是父进程fork出两个子进程,子进程1需要给子进程2发送一个数据,然后子进程2再将这个数据发送给父进程 具体思想如下. 父进程fork出来的子进程的pid,只有父进程保存下来了, ...

  6. CS2013调试DLL

    需要打开两个项目,一个是Win32Project1,由这个项目创建DLL,注意要在DLL函数前加上__declspec(dllexport),这样就会还配套生成一个.lib 然后再打开一个项目,一般为 ...

  7. fackbook的Fresco的多种图片加载方法以及解码过程

    上篇文章中我们提到了图片加载其实是用了三条线程,如果没看过的同学可以先了解下这里. fackbook的Fresco的Image Pipeline以及自身的缓存机制 那么今天我们就来探索一下如何在代码中 ...

  8. python pdb

    pdb 以参数-m pdb启动后,pdb定位到下一步要执行的代码-> s = '0'.输入命令l来查看代码: 输入命令n可以单步执行代码: 任何时候都可以输入命令p 变量名来查看变量: (Pdb ...

  9. linux 压缩文件的命令总结

    Linux压缩文件的读取 *.Z       compress 程序压缩的档案: *.bz2     bzip2 程序压缩的档案: *.gz      gzip 程序压缩的档案: *.tar     ...

  10. Swift常量和变量

    常量和变量由一个特定名称来表示,如maxNumber 或者 message.常量所指向的是一个特定类型的值, 如数字10或者字符”hello”.变量的值可以根据需要不断修改,而常量的值是不能够被二次修 ...