uva 1152 - 4 Values whose Sum is 0

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 ) 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 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 line is followed by a blank line, and there is also a blank line between two 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 have 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 test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

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

Sample Input

1


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

Sample Output

5

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

解題策略

方法一 upper_bound lower_bound(uva解題約5秒)

a[]與b[]相加後每個數儲存到ab[],排序ab[]

將每一個(-c[]-d[])的值到ab[]中找尋該值的lower_bound與upper_bound,

upper_bound減去lower_bound就是該值在ab[]的個數

lower_bound(ab,ab+len,k)找到最小大於等於k的由左到右第一個遇到的元素的指標

upper_bound(ab,ab+len,k)找到最小大於k的由左到右第一個遇到的元素的指標

方法二 兩個排序陣列(uva解題約4秒)

a[]與b[]相加後每個數儲存到ab[]

-c[]與-d[]相加後每個數儲存到cd[]

排序ab[]與cd[]

經由將ab[]每個元素使用二元搜尋cd[]中相同值個數累加起來就是答案

方法三 自己寫Hash(uva解題1秒以內)

a[]與b[]相加後每個數儲存到hash ab,

將每一個(-c[]-d[])的值到hash ab中找尋該值的個數,

方法一 upper_bound lower_bound(uva解題約5秒)

方法二 兩個排序陣列(uva解題約4秒)

方法三 自己寫Hash(uva解題1秒以內)