4 Values whose Sum is 0
Time Limit: 15000MS Memory Limit: 228000K
Total Submissions: 19322 Accepted: 5778
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的数组,求四个数组各取一个元素和为0的取法有几种(考虑顺序)

首先有两种做法,一种二分,一种哈希

看一眼数据规模知道枚举每一位的O(n^4)绝对超时

所以观察题目,发现只要求和为0,那么考虑只枚举出前两个数组的任意元素和与后两个数组任意元素和

这样再枚举一遍前两个数组的任意元素和,检查是否有对应元素和为0即可

接下来还要再优化

二分写得很简单,具体代码在《挑战程序设计竞赛》第23页不想写

再来看哈希

把两数组的元素和得出一个键值(就是哈希值)接下来链式储存进去

什么叫链式呢?就是和邻接表差不多

把哈希映射的数组当成head数组,把原值当成边,存原值和next也就是哈希值相同的下一个值的为止

注意这道题还得再优化,多存一个同一数值的重复次数而不是分开存,不然会MLE没错这是省空间用的

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 const int mod=;
 5 typedef struct{
 6         int val;
 7         int num;
 8         int next;
 9 }node;
 int data[][];
 int n,tot=;
 int hash[mod+];
 node all[];
 int abs(int num){
     return num>?num:(-)*num;//考虑负数!考虑负数!考虑负数!
 }
 int get(int num){
     return (abs(num))%mod;
 }
 int add(int num){
     int tmp=get(num);
     int p=;
     if(hash[tmp]){
        for(p=hash[tmp];p!=;p=all[p].next){
            if(all[p].val==num){
               all[p].num++;
               break;
            }
        }
     }
     if((!hash[tmp])||(p==)){
           all[++tot].val=num;
           all[tot].num=;
           all[tot].next=hash[tmp];
           hash[tmp]=tot;
     }
     return ;
 }
 int find(int num){
     int tmp=get(num);
     int p;
     for(p=hash[tmp];p;p=all[p].next){
         if(all[p].val==num)return all[p].num;
     }
     return ;
 }
 int main(){
     memset(hash,,sizeof(hash));
     memset(all,,sizeof(all));
     int n;
     scanf("%d",&n);
     for(int i=;i<=n;i++)scanf("%d %d %d %d",&data[i][],&data[i][],&data[i][],&data[i][]);
     for(int i=;i<=n;i++)for(int j=;j<=n;j++)add(data[i][]+data[j][]);
     int ans=;
     for(int i=;i<=n;i++)for(int j=;j<=n;j++)ans+=find(-(data[i][]+data[j][]));
     printf("%d",ans);
     return ;

58 }

[poj2785]4 Values whose Sum is 0(hash或二分)的更多相关文章

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

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

  2. POJ-2785 Values whose Sum is 0 Hash表

    题目链接:https://cn.vjudge.net/problem/POJ-2785 题意 给出四组数,每组有n个数 现从每组数中取一个数作为a,b,c,d 问有几组这样的a+b+c+d=0 思路 ...

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

  4. POJ2785 4 Values whose Sum is 0 (二分)

    题意:给你四组长度为\(n\)序列,从每个序列中选一个数出来,使得四个数字之和等于\(0\),问由多少种组成情况(仅于元素的所在位置有关). 题解:\(n\)最大可以取4000,直接暴力肯定是不行的, ...

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

  6. POJ 2785 4 Values whose Sum is 0

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

  7. 折半枚举(双向搜索)poj27854 Values whose Sum is 0

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

  8. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  9. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

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

随机推荐

  1. Spring+Mybatis整合报错Mapped Statements collection does not contain value原因之一

    报错如下: ### Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements coll ...

  2. pipe row的用法, Oracle split 函数写法.

    为了让 PL/SQL 函数返回数据的多个行,必须通过返回一个 REF CURSOR 或一个数据集合来完成.REF CURSOR 的这种情况局限于可以从查询中选择的数据,而整个集合在可以返回前,必须进行 ...

  3. 单利 复利计算器程序1.0 2.0 3.0 [ 合 ] 之 C语言

    本程序用C语言编写~~~ 1.计算:本金为100万,利率或者投资回报率为3%,投资年限为30年,那么,30年后所获得的利息收入:按复利计算公式来计算就是:1,000,000×(1+3%)^30 1 v ...

  4. 【20160924】GOCVHelper 图像增强部分(3)

    //顶帽去光差,radius为模板半径     Mat moveLightDiff(Mat src,int radius){         Mat dst;         Mat srcclone ...

  5. 深入理解include预编译原理

    http://ticktick.blog.51cto.com/823160/596179 你了解 #include 某个 .h 文件后,编译器做了哪些操作么? 你清楚为什么在 .h文件中定义函数实现的 ...

  6. JAVA基础知识之JVM-——使用反射生成并操作对象

    Class对象可以获取类里的方法,由Method对象表示,调用Method的invoke可以执行对应的方法:可以获取构造器,由Constructor对象表示,调用Constructor对象的newIn ...

  7. 浅谈Apache性能调优

    做了很多WEB系统性能测试,都知道了解测试环境,服务器硬件配置,web服务器参数配置是我们开始测试前首先要做的事情. 针对并发数量来说,不同数量级的用户并发,需求的服务器和web服务参数肯定是不同的. ...

  8. nylg 小M的因子和

    小M的因子和 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 小M在上课时有些得意忘形,老师想出道题目难住他.小M听说是求因子和,还是非常得意,但是看完题目是求A的B ...

  9. CentOS搭建Redis集群

    集群原理-redis-cluster架构图 架构细节: (1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. (2)节点的fail是通过集群中超过半数的 ...

  10. 自己模拟实现spring IOC原理

    1.1.IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对 ...