J Midway meeting Method, summation

Source: Internet
Author: User

---restore content starts---

J-Midway Meeting MethodTime limit:9000MS Memory Limit:0KB 64bit IO Format:%lld &%llu SubmitStatus

Description

The SUM problem can formulated as Follows:given four lists A, B, C, D<tex2h  Tml_verbatim_mark> of integer values, compute how many quadruplet (a, b, c, D ) A x B x C x d<tex2html_verbatim_mark> is such that a + b + c + D = 0<tex2html_verbatim_mark>. In the following, we assume this all lists has the same size n<tex2html_verbatim_mark>.

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 was followed by a blank line, and there was also a blank line between the consecutive inputs.


The first line of the input file contains the size of the lists n<tex2html_verbatim_mark> (this value C An IS as large as 4000). We then had n<tex2html_verbatim_mark> lines containing four integer values (with absolute value as Lar GE as 228<tex2html_verbatim_mark>) that belong respectively to A, B, C< Tex2html_verbatim_mark> and D<tex2html_verbatim_mark>.

Output

For each test case, the output must follow the description below. The outputs of the consecutive cases would be separated to a blank line.


For each of the input file, your program have to write the number quadruplets whose sum is zero.

Sample Input

16-45 22 42-16-41-27 56 30-36 53-37 77-36 30-75-4626-38-10 62-32-54-6 45

SAmple Output

5

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

Topic Ideas: To find a number for each column, and to get a sequence of 0, there are several different scenarios analysis: If the four-cycle, there is no doubt that the time-out, you need to use the enumeration case, the number of 1, 2 columns for one and, 3, 4 columns of the number of a and, and then the binary search program code
#include <cstdio>#include<algorithm>using namespacestd;inta[16000010];intb[16000010];intp[4010][4];intT;intErfen (intx) {    intCnt=0; intL=0, r=t-1, Mid;  while(r>l) {mid= (l+r) >>1; if(b[mid]>=x) r=mid; ElseL=mid+1; }     while(b[l]==x&&l<t) {cnt++; L++; }    returnCNT;}intMain () {intn,i,j,t; Long LongRes; scanf ("%d",&T);  while(t--) {scanf ("%d",&N); Res=0;  for(i=0; i<n;i++)             for(j=0;j<4; j + +) scanf ("%d",&P[i][j]); T=0;  for(i=0; i<n;i++)             for(j=0; j<n;j++) A[t++]=p[i][0]+p[j][1]; Sort (A,a+t); T=0;  for(i=0; i<n;i++)             for(j=0; j<n;j++) B[t++]=p[i][2]+p[j][3]; Sort (b,b+t);  for(i=0; i<t;i++) Res+=erfen (-A[i]); printf ("%d\n", RES); if(T) printf ("\ n"); }    return 0;}
View Code

---restore content ends---

J Midway meeting Method, summation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.