PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000061. Đếm bộ bốn có tổng bằng không (4 values whose sum is 0)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm bộ bốn có tổng bằng không (4 values whose sum is 0)

4 values whose sum is 0

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Four integer arrays A,B,C,DA,B,C,D are given, each containing exactly nn elements. Input row ii gives the four values Ai,Bi,Ci,DiA_i,B_i,C_i,D_i.

Choose one index from each array independently, forming (i,j,k,l)(i,j,k,l) with 1≤i,j,k,l≤n1\le i,j,k,l\le n. It qualifies if Ai+Bj+Ck+Dl=0A_i+B_j+C_k+D_l=0. Indices in different arrays may have the same numerical position; equal values do not merge distinct index quadruples.

Count all qualifying quadruples.

Input

  • The first line contains integer nn: the size of each array.
  • Each of the next nn lines contains four integers Ai,Bi,Ci,DiA_i,B_i,C_i,D_i, in that order.

Output

Print one integer: the number of index quadruples with sum zero.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; −228≤Ai,Bi,Ci,Di≤228-2^{28}\le A_i,B_i,C_i,D_i\le2^{28} for all 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le 150; −228≤Ai,Bi,Ci,Di≤228-2^{28}\le A_i,B_i,C_i,D_i\le2^{28} for all 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤25001\le n\le 2500; −228≤Ai,Bi,Ci,Di≤228-2^{28}\le A_i,B_i,C_i,D_i\le2^{28} for all 1≤i≤n1\le i\le n.

Examples

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

Explanation

The five valid index quadruples (i,j,k,l)(i,j,k,l) are (1,2,1,2)(1,2,1,2), (5,4,5,4)(5,4,5,4), (6,1,2,4)(6,1,2,4), (6,4,4,3)(6,4,4,3), and (6,6,2,2)(6,6,2,2). Their sums are −45−27+42+30=0-45-27+42+30=0, 26+30−10−46=026+30-10-46=0, −32+22+56−46=0-32+22+56-46=0, −32+30−75+77=0-32+30-75+77=0, and −32−54+56+30=0-32-54+56+30=0. Each quadruple picks one index from each of the four arrays, and there are no other valid choices. Thus the answer is 5.