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 formulated as Follows:given four lists A, B, C, D of an integer values, compute how many quadruplet ( A, B, C, D) ∈a x B x C x D is such that A + B + c + d = 0. In the following, we assume this all lists has 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 had 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 of the input file, your program have to write the number quadruplets whose sum is zero.
Sample Input
Sample Output
Hint
Sample Explanation:indeed, the sum of the five following quadruplets is zero: (-45,-27, 42, 30), (26, 30,-10,-46), (-3) 2, 22, 56,-46), (-32, 30,-75, 77), (-32,-54, 56, 30).
(is not first translated) test instructions is probably given four arrays of length n, four arrays take one element and 0 of the methods have several (consider order)
First there are two approaches, a two-point, a hash
Take a look at the data size know enumeration of each bit O (n^4) Absolute timeout
So observe the topic and find only the requirements and for 0, then consider only enumerating any elements of the first two arrays and with the latter two arrays of arbitrary elements and
This again enumerates any elements of the first two arrays and checks for the corresponding elements and 0.
And then we're going to optimize
The two points are very simple, the specific code in the Challenge Program Design Contest 23rd page
do not want to write
Look at the hash again.
Put the elements of the two array and derive a key value (that is, the hash value) next to the chain to store in
What do you mean, chain-like? is similar to the adjacency table.
The hash map array is treated as a head array, the original value as an edge, and the original value is the next value with the same hash value.
Note that this problem has to be optimized again, save a number of repetitions of the same value instead of being separated, or it will be mle yes, it's space-saving.
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4ConstintMod=1000007;
5typedefstruct{
6intVal
7intNum
8 intNext
9}node;
Tenintdata[4005][4];
Oneintn,tot=0;
AintHash[mod+1];
-Node all[16000000];
-intAbsintNUM) {
thereturnNum>0? Num: (-1) *num;//consider negative numbers! Consider negative numbers! Consider negative numbers!
-}
-intGet(intNUM) {
-return(num)%mod;
+}
-intAddintNUM) {
+inttmp=Get(num);
Aintp=1;
atif(Hash[tmp]) {
- for(P=hash[tmp];p! =0;p =all[p].next) {
- if(All[p].val==num) {
-all[p].num++;
- Break;
-}
in}
-}
to if((!hash[tmp]) | | (p==0)){
+All[++tot].val=num;
-all[tot].num=1;
theALL[TOT].NEXT=HASH[TMP];
*Hash[tmp]=tot;
$}
Panax Notoginsengreturn0;
-}
theintFindintNUM) {
+inttmp=Get(num);
A intP
the for(P=hash[tmp];p; p=all[p].next) {
+if(All[p].val==num)returnAll[p].num;
-}
$return0;
$}
-intMain () {
-memset (Hash,0,sizeof(hash));
thememset (All,0,sizeof(all));
-intN
Wuyiscanf"%d", &n);
the for(intI=1; i<=n;i++) scanf ("%d %d%d%d", &data[i][1],&data[i][2],&data[i][3],&data[i][4]);
- for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) Add (data[i][1]+data[j][2]);
Wu intans=0;
- for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) Ans+=find (-(data[i][3]+data[j][4]));
Aboutprintf"%d", ans);
$return0;
- }
[Poj2785]4 Values whose Sum is 0 (hash or dichotomy)