POJ 2785 4 Values whose Sum is 0 (binary enumeration)

Source: Internet
Author: User

4 Values whose Sum is 0
Time Limit: 15000MS Memory Limit: 228000K
Total Submissions: 17088 Accepted: 4998
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

6-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



Idea: first two columns of enumeration 22 to produce the number of n*n, and then two columns after enumeration, and then two points can be equal to 0 of the number of good complexity is O (N*NLOGN)



#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <sstream> #include <string># Include <vector> #include <cstdio> #include <ctime> #include <bitset> #include <algorithm > #define SZ (x) ((int) (x). Size ()) #define ALL (v) (v). Begin (), (v). End () #define foreach (I, v) for (__typeof ((v). Begin () ) i = (v). Begin (); I! = (v). End (); + + i) #define Refeach (i, v) for (__typeof ((v). Rbegin ()) i = (v). Rbegin (); I! = (v). rend (); + + i) #define REP (i,n) for (int I=1; I<=int (n);    i++) using namespace std;typedef long long ll;const int N = 4000+100;int a[5][n];int all[n*n];int half[n*n];int main () {    int n;        while (~SCANF ("%d", &n)) {Rep (i,n) Rep (j,4) scanf ("%d", &a[j][i]);        int top = 0;        ll ans = 0;        Rep (I,n) Rep (j,n) half[++top] = A[1][i]+a[2][j];    Rep (I,n) Rep (j,n) all[top--] = A[3][i]+a[4][j];    Sort (all+1,all+n*n+1);        REP (i,n*n) ans + = Upper_bound (All+1,all+n*n+1,-half[i])-lower_bound (All+1,all+n*n+1,-half[i]);    printf ("%i64d\n", ans); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2785 4 Values whose Sum is 0 (binary enumeration)

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.