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. java collections读书笔记(8)collection框架总览(1)

  2. C++Primer 第十七章

    //1.当我们希望将一些数据组合成单一对象,但又不想麻烦地定义一个新的数据结构来表示这些数据的时候,tuple非常有用.其和其伴随类型和函数都定义在头文件tuple中,声明在命名空间std中. tup ...

  3. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  4. bat file handling, main: echo type *.txt >> />

    echo.@.call.pause.rem(小技巧:用::代替rem)是批处理文件最常用的几个命令 echo 表示显示此命令后的字符  echo off 表示在此语句后所有运行的命令都不显示命令行本身 ...

  5. 开发者经验谈:如何一天时间搞定iOS游戏开发?

    开发者经验谈:如何一天时间搞定iOS游戏开发? 在一天时间里将完成iPhone游戏开发由梦想变为现实? 本文作者给出了从创意转变成现实的详细答案.使用苹果原生游戏引擎SpriteKit,遵循一定的原则 ...

  6. [转]iis7.5+win2008 出现 HTTP Error 503. The service is unavailable.

    解决: 应用程序池启动32位应用程序 设置托管管道为集成 (仍然有问题) 试试以下方法: http://phpwind.me/1222.html 楼主  发表于: 2011-11-26     图片: ...

  7. BackgroundWorker的使用

    一个程序中需要进行大量的运算,并且需要在运算过程中支持用户一定的交互,为了获得更好的用户体验,使用BackgroundWorker来完成这一功能.   基本操作: bgw.RunWorkerAsync ...

  8. 夺命雷公狗—angularjs—9—ng-class的自定义函数的用法

    angularjs里面其实给我们留下了一个很不错的地方,他就是可以直接调用函数从而对该位置进行处理, 被点击后展示效果如下所示: 开始走代码吧.... <!doctype html> &l ...

  9. Bug测试报告--俄罗斯方块--新蜂

    项目名:俄罗斯方块 组名:新蜂 测试者:韩媛媛(nice!团队) 用户需求规格说明书URL:http://www.cnblogs.com/Boxer1994/p/6084035.html 组长博客UR ...

  10. 【ibus】设置ibus输入法(pinyin & sunpinyin)

    设置ibus-pinyin 在终端中运行 /usr/lib/ibus-pinyin/ibus-setup-pinyin 命令可以调出ibus的完整设置对话框 设置ibus-sunpinyin 可以执行 ...