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. Dynamics AX 2012 R2 在增强入站端口中找不到自定义服务操作

        Reinhard写好自定义服务A,添加好服务操作A1,A2,A3.....     然后,Reinhard在增强的入站端口,选择服务操作时,却找不到这些A1,A2,A3.     查找相关资料 ...

  2. jqGrid中选择的行的数据[转]

    如何获取jqGrid中选择的行的数据? 下面可以获取选择一行的id,如果你选择多行,那下面的id是最后选择的行的id: var id=$(‘#gridTable’).jqGrid(‘getGridPa ...

  3. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:2.3.1:jar (default-jar) on

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:2.3.1:jar (default-jar) on ...

  4. ios app的真机调试与发布配置

    1.打开应用程序—>[钥匙串访问]—>[证书助理]—>[从证书办法机构请求证书]     2.在[用户电子邮件地址]填入apple账户用的邮箱,选择[存储到磁盘],点击[继续],会在 ...

  5. mongo 学习笔记

    mysql语句 : ' ,,),(,,)   mongo语句: db.}}).limit() db."}) db.}}) 条件操作符1 mongodb中的条件操作符有: (>) 大于 ...

  6. QQ空间开放平台开发教程-SDK和API的使用

    <?php /** * OpenAPI V3 SDK 示例代码,适用于大部分OpenAPI.如果是上传文件类OpenAPI,请参考本SDK包中的“Test_UploadFile.php”文件中的 ...

  7. aspnet excel导入导出SQLserver

    http://my.csdn.net/libin690145955/code/detail/452 http://blog.csdn.net/ltoper/article/details/532980 ...

  8. 《C++标准库》

    函数对象 使用bind时注意,占位符有自己的命名空间std::placeholders,如果不在程序开始处using std::placeholders,那么就要写成: std::bind(std:: ...

  9. WebView的返回功能

    WebView 实现返回到最后一个 在退出 import android.app.Activity; import android.os.Bundle; import android.view.Key ...

  10. Fibonacci Again 分类: HDU 2015-06-26 11:05 13人阅读 评论(0) 收藏

    Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...