K-4 Values whose Sum is 0 (Midway encounter method)

Source: Internet
Author: User

K-4 Values whose Sum is 0 crawling in process ... crawling failed time limit:9000MS Memory Limit:0KB 64bit IO format:%lld &%llu Submit Status Practice UVA 1152 Appoint Description:System Crawler (2015-03-12)

Description

 

The SUM problem can formulated as Follows:given four lists A, B, C, D of Integ Er values, compute how many quadruplet (a, b, c, D ) ax 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 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 (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 re spectively to A, B, C and D .

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

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <stack>9#include <queue>Ten#include <Set> One#include <map> A#include <list> -#include <iomanip> -#include <cstdlib> the#include <sstream> - using namespacestd; -typedefLong LongLL; - Const intinf=0x5fffffff; + Const Doubleexp=1e-8; - Const intms=4005; + inta[ms],b[ms],c[ms],d[ms],n,sum[ms*MS]; A  at intMain () - { -     intT; -scanf"%d",&T); -      while(t--) -     { inscanf"%d",&n); -          for(intI=0; i<n;i++) toscanf"%d%d%d%d",&a[i],&b[i],&c[i],&d[i]); +         intCnt=0; -          for(intI=0; i<n;i++) the              for(intj=0; j<n;j++) *sum[cnt++]=a[i]+B[j]; $Sort (sum,sum+CNT);Panax NotoginsengLL ans=0; -          for(intI=0; i<n;i++) the              for(intj=0; j<n;j++) +         { AAns+=upper_bound (Sum,sum+cnt,-c[i]-d[j])-lower_bound (sum,sum+cnt,-c[i]-d[j]); the         } +printf"%lld\n", ans); -         if(T) $printf"\ n"); $     } -     return 0; -}

K-4 Values whose Sum is 0 (Midway encounter 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.