The SUM problem can 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 is such that a+b+c+d = 0. In the following, we
Assume that all lists has 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. followed by a blank line, and there is also a
Blank line between 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 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 test case, the your program have to write the number quadruplets whose sum is zero.
The outputs of the consecutive cases would be separated to a blank line.
1#include <cstdio>2#include <cstring>3 intAbsintx)4 {5 if(x>=0)returnx;6 return-x;7 }8 Const intm=1098469;9 inta[4010],b[4010],c[4010],d[4010],first[1100000],next[17000000],num[17000000];Ten intMain () One { A intI,j,k,n,p,q,x,y,z,t,ans; -scanf"%d",&t); - while(t--) the { -Memset (A,0,sizeof(a)); -memset (b,0,sizeof(b)); -Memset (c,0,sizeof(c)); +memset (D,0,sizeof(d)); -memset (First,0,sizeof(first)); +Memset (Next,0,sizeof(next)); Amemset (NUM,0,sizeof(num)); atans=0; -scanf"%d",&n); - for(i=1; i<=n;i++) -scanf"%d%d%d%d",&a[i],&b[i],&c[i],&d[i]); - for(i=1; i<=n;i++) - for(j=1; j<=n;j++) in { -x=a[i]+B[j]; toP=abs (%m); +next[(I-1) *n+j]=First[p]; -first[p]= (I-1) *n+J; thenum[(I-1) *n+j]=x; * } $ for(i=1; i<=n;i++)Panax Notoginseng for(j=1; j<=n;j++) - { thex=-c[i]-D[j]; +P=abs (%m); A for(k=first[p];k;k=Next[k]) the if(X==num[k]) ans++; + } -printf"%d\n", ans); $ if(t) printf ("\ n"); $ } -}
Enumerate the a+b, save all the values, and then enumerate the-c-d, and find them in a+b.
The specific lookup method is a hash, except for the K-take method.
UVA 1152 4 values whose sum is ZERO--YHX