The fourth week of training (high efficiency algorithm design) J Question (Midway meeting method)

Source: Internet
Author: User

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

If according to the original method, the four-cycle, there is no doubt will be timed out, can only use the method to turn the four-cycle double, but each of the largest number of up to 2 of the 28-square, using the short open array is not feasible, do a large integer hash. No, you can only use the array to save the values, and then look for ...

#include"Iostream"#include"CString"#include"Algorithm"#include"Map"#include"Cmath"UsingNamespace Std;Constint MAXN=4000+10;int book[16000010];int a[MAXN],b[MAXN],c[MAXN],d[MAXN];IntMain(){int T; Cin>>t;While(T--){int n; Cin>>n;Memset(Book,0,sizeof(Book));For(int I=0; I<n; I+ +) CIN>>a[I]>>b[I]>>c[I]>>d[I];int F=0;For(int IA=0; IA<n; IA++)For(int IB=0; IB<n; IB++){Book[F++]=-(A[IA]+b[IB]);}int sum=0;Sort(Book, book+f);For(int IC=0=i%<n=i%++)For(int ID=0; ID<n; ID++){int Temp=c[IC]+d[ID];int x1=Lower_bound(Book, book+f, temp)-bookint x2=upper_bound  (Book,book+f Temp+=x2 -x1;  Cout<<sum<<endlif (T< <endl return 0;< Span class= "Sh-cbracket" >             

The fourth week of training (high efficiency algorithm design) J Question (Midway meeting method)

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.