Sphere Online Judge

Computer geeks! Show off your skills and claim the job of your dreams! RecruitCoders.com

SPOJ Problem Set (classical)

1296. 4 values whose sum is 0

Problem code: SUMFOUR

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) belongs to A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have 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 have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .

Output

Output should be printed on a single line.

Example

Input:
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Output:
5

Added by:Abhilash I
Date:2007-02-06
Time limit:10s
Source limit:50000B
Languages:All except: ERL JS PERL 6
Resource:South western 05-06

hide comments
2012-04-27 12:19:14 teoy
My algo is O(n^2)*log(n^2),it used 16s.Who can tell me what a algorithm is that use only 1second
2012-04-23 00:33:50 Nic Roets
I printed the result as a 32 bit int and got AC. That means there are no test cases where the answer is more than 2^32. Please add some !

Last edit: 2012-04-23 09:13:14
2012-02-26 12:59:46 BlackBird
For strange reasons, my implementation with arrays was getting TLE. But the same one with Vectors got AC!. Don't know why :(
2011-12-19 13:40:41 The new guy
tle :(

Last edit: 2011-12-19 13:58:42
2011-12-18 15:20:37 bristy
finally got AC!
2011-12-18 13:15:26 Bharath Reddy
I am getting an NZEC even though my code gives correct output for the input given above. I think this is because I am using arrays....
@Brajesh: What did you use to store your numbers? What was your space complexity?

Last edit: 2011-12-19 04:28:03
2011-10-06 19:05:15 `
I think that qsort is a bit faster than sort() ,,just somewhat tough to code
2011-09-19 17:09:11 Javier Sardiñas
i wrote a O(n^2logn) solution and still got TLE, i use stl maps, are they that slow?
2011-09-02 00:33:45 Chandan Giri
use vectors in c++
2011-06-08 02:15:24 Nghia Nguyen
It is recommended to be at most O(n^2logn).

Last edit: 2011-06-08 02:15:40
SPOJ System © 2012 Sphere Research Labs | Projekty informatyczne i aplikacje na zamówienie. All Rights Reserved.