uva 1152 4 values whose sum is zero ——yhx
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) 2 AB C D are such that a+b+c+d = 0. In the following, we
assume that all lists have the same size n.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases
following, each of them as described below. This line is followed by a blank line, and there is also a
blank line between two consecutive inputs.
The rst line of the input le 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 test case, your program has to write the number quadruplets whose sum is zero.
The outputs of two consecutive cases will be separated by a blank line.
#include<cstdio>
#include<cstring>
int abs(int x)
{
if (x>=) return x;
return -x;
}
const int m=;
int a[],b[],c[],d[],first[],next[],num[];
int main()
{
int i,j,k,n,p,q,x,y,z,t,ans;
scanf("%d",&t);
while (t--)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(c,,sizeof(c));
memset(d,,sizeof(d));
memset(first,,sizeof(first));
memset(next,,sizeof(next));
memset(num,,sizeof(num));
ans=;
scanf("%d",&n);
for (i=;i<=n;i++)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for (i=;i<=n;i++)
for (j=;j<=n;j++)
{
x=a[i]+b[j];
p=abs(x%m);
next[(i-)*n+j]=first[p];
first[p]=(i-)*n+j;
num[(i-)*n+j]=x;
}
for (i=;i<=n;i++)
for (j=;j<=n;j++)
{
x=-c[i]-d[j];
p=abs(x%m);
for (k=first[p];k;k=next[k])
if (x==num[k]) ans++;
}
printf("%d\n",ans);
if (t) printf("\n");
}
}
枚举a+b,把所有值存起来,然后枚举-c-d,在a+b中查找。
具体查找方法是哈希,除k取余法即可。
uva 1152 4 values whose sum is zero ——yhx的更多相关文章
- UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)
4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...
- 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 ...
- UVA - 1152 4 Values whose Sum is 0(中途相遇法)
题意:从四个集合各选一个数,使和等于0,问有多少种选法. 分析:求出来所有ai + bi,在里面找所有等于ci + di的个数. #pragma comment(linker, "/STAC ...
- UVa 1152 4 Values whose Sum is 0
题意:给出n,四个集合a,b,c,d每个集合分别有n个数,分别从a,b,c,d中选取一个数相加,问使得a+b+c+d=0的选法有多少种 看的紫书,先试着用hash写了一下, 是用hash[]记录下来a ...
- UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...
- UVA - 1152 4 Values whose Sum is 0问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- UVA - 1152 --- 4 Values whose Sum is 0(二分)
问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
随机推荐
- 在phpwind内容页使用百度分享进行图片分享
在phpwind内容页使用百度分享进行图片分享时,百度分享默认提取到的图片不一定是主题正文内容中的图片,需要使用百度提供的配置机制自行调整. 整个代码添加的位置在此不论,主要原理是在主题正文区域提取图 ...
- Maven创建webapp(二)
这一节将记录在myeclipse下用maven创建一个简单的webapp项目 web开发maven仓库自动添加组件,故需要需要保持网络的通畅. 打开myeclipse --> File - ...
- java微信开发(wechat4j)——wechat4j配置文件解读
wechat4j的配置文件是wechat4j.properties.需要放置在项目src目录下.在wechat4j.jar中的META-INF下有一个wechat4j.properties.sampl ...
- Javascript-回调函数浅谈
回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直接调用,而是在特定 ...
- 趣味题:恺撒Caesar密码(c++实现)
描述:Julius Caesar 生活在充满危险和阴谋的年代.为了生存,他首次发明了密码,用于军队的消息传递.假设你是Caesar 军团中的一名军官,需要把Caesar 发送的消息破译出来.并提供给你 ...
- JQuery EasyUI Tree
Tree 数据转换 所有节点都包含以下属性: id:节点id,这个很重要到加载远程服务器数据 which is important to load remote data text: 显示的节点文本 ...
- KindEditor编辑器在ASP.NET中的使用
KindEditor编辑器在ASP.NET中的使用 最近做的项目中都有用到富文本编辑器,一直在寻找最后用的富文本编辑器,之前用过CKEditor,也用过UEditor,这次打算用 一下KindEdit ...
- Atitit.图片木马的原理与防范 attilax 总结
Atitit.图片木马的原理与防范 attilax 总结 1.1. 像图片的木马桌面程序1 1.2. Web 服务端图片木马1 1.3. 利用了Windows的漏洞1 1.4. 这些漏洞不止Windo ...
- 转:Web应用程序项目XX已配置为使用IIS
转:http://www.cnblogs.com/Joetao/articles/2392526.html 今天在看开源项目Umbraco是,出现一个项目加载不了,并报如下错误: Web应用程序项目U ...
- 转:EClipse 10个最有用的快捷键
Eclipse快捷键 10个最有用的快捷键 Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代 ...