Ruminations on Ruminants
Contest: Codeforces Round 334 (Div. 1)
Difficulty rating: 2900
Tags: geometry, math

Kevin Sun is ruminating on the origin of cows while standing at the origin of the Cartesian plane. He notices n lines $$\ell_{1}, \ell_{2}, \ldots \ell_{n}$$ on the plane, each representable by an equation of the form ax + by = c. He also observes that no two lines are parallel and that no three lines pass through the same point.

For each triple (i, j, k) such that 1 ≤ i < j < k ≤ n, Kevin considers the triangle formed by the three lines $$\ell_{i}, \ell_{j}, \ell_{k}$$ . He calls a triangle original if the circumcircle of that triangle passes through the origin. Since Kevin believes that the circles of bovine life are tied directly to such triangles, he wants to know the number of original triangles formed by unordered triples of distinct lines.

Recall that the circumcircle of a triangle is the circle which passes through all the vertices of that triangle.

Input format:
The first line of the input contains a single integer n (3 ≤ n ≤ 2000), the number of lines.

The next n lines describe lines $$\ell_{1}, \ell_{2}, \ldots \ell_{n}$$. The i-th of these lines contains three space-separated integers ai, bi, ci (|ai|, |bi|, |ci| ≤ 10 000, ai2 + bi2 > 0), representing the equation aix + biy = ci of line $$\ell_{\overline{2}}$$.

Output format:
Print a single integer, the number of triples (i, j, k) with i < j < k such that lines $$\ell_{i}, \ell_{j}, \ell_{k}$$ form an original triangle.

Constraints:
Time limit: 4.0 s
Memory limit: 256.0 MB

Note:
Note that in the first sample, some of the lines pass through the origin.

In the second sample, there is exactly one triple of lines: y = 1, x + y = 2, x - y =  - 2. The triangle they form has vertices (0, 2), (1, 1), ( - 1, 1). The circumcircle of this triangle has equation x2 + (y - 1)2 = 1. This indeed passes through (0, 0).

Example input:
4
1 0 0
0 1 0
1 1 -1
1 -1 2

Example output:
2

Example input:
3
0 1 1
1 1 2
1 -1 -2

Example output:
1

Editorial:
Hint: It seems like this would be O(n3) because of triples of lines. Can you reduce that with some geometric observations? Think of results from Euclidean geometry relating to 4 points lying on the same circle.
First, we will prove a lemma, known as the Simson Line:
Lemma:
Given points A, B, C, P in the plane with D, E, F on lines BC, CA,  and AB, respectively such that $$PD \perp BC, PE \perp AC, PF \perp AB$$, then P lies on the circumcircle of $$\triangle ABC$$ if and only if D, E,  and F are collinear.
![ ](http://i.imgur.com/yQGnVba.png)
Proof:
Assume that the points are configured as shown, and other other configurations follow similarly. Recall that a quadrilateral ABCP is cyclic if and only if $$\angle B A C = 180^{\circ} - \angle B P C$$. Note that this implies that a quadrilateral with two opposite right angles is cyclic, so in particular quadrilaterals AEPF, BFPD, CDPE are cyclic. Because
Now note that $$\angle F P B = \angle F D B$$ (again since BFPD is cyclic) and $$\angle EPC = \angle EDC$$, so $$\angle BDF = \angle EDC$$ if and only if $$\angle FPB = \angle EPC$$, if and only if ABPC is cyclic. Thus the lemma is proven.
This lemma provides us with an efficient way to test if the circumcircle of the triangle formed by three lines in the plane passes through the origin. Specifically, for a line $$\ell_{\overline{2}}$$, let Xi be the projection of the origin onto $$\ell_{\overline{2}}$$. Then $$\ell_{i}, \ell_{j}, \ell_{k}$$ form an original triangle if and only if Xi, Xj,  and Xk are collinear. Thus the problem reduces to finding the number of triples i < j < k with Xi, Xj, Xk collinear. The points Xi are all distinct, except that possibly two lines may pass through the origin, so we can have up to two points Xi at the origin.
Let us first solve the problem in the case that all points Xi are distinct. In this case, consider each i, and store for each slope how many points Xj the line Xi Xj has this slope. This storage can be done in O(1) or $${\mathcal {O}}(\log n)$$, depending on how hashing is done. Note also that we must consider a vertical line to have the valid slope $${ \frac { 1 } { 0 } }$$. If $${ \alpha } _ { 1 }, { \alpha } _ { 2 }, \cdots { \alpha } _ { e }$$ are the number of points corresponding to the distinct slopes $$s_{1}, s_{2}, \ldots s_{e}$$ through Xi, then for Xi we add to the total count
But after our count has cycled through all values of Xi, we must divide by 3, as each triple i < j < k has been counted 3 times.
If the Xi are not distinct, we only encounter an issue in the above solutions when we consider the slope through points Xi and Xj where Xi = Xj. In this case, for any third k, Xi, Xj,  and Xk are collinear. So when considering slopes from Xi in the original algorithm, we simply run the algorithm on all slopes other than the one through Xj, and simply add n - 2 to the count afterwards to account for the n - 2 points which are collinear with Xi and Xj.
Running the above, we get an algorithm that runs in O(n2) or $$O(n^2 \log n)$$.