Bovine Embroidery

No attempts yetTime limit1sMemory limit128 MB

Problem

Bessie has taken up the detailed art of bovine embroidery. Cows embroider a cloth mounted in a circular hoop of integer radius $d$ ($1 \le d \le 50{,}000$). They sew $N$ ($2 \le N \le 50{,}000$) threads, each running in a straight line from one point on the edge of the hoop to another point on the edge of the hoop. No two thread endpoints share the same location on the hoop's edge.

Being mathematically inclined, Bessie describes each thread by a line equation of the form $ax + by + c = 0$. The coefficients $a$, $b$, and $c$ are integers with $-1{,}000{,}000 \le a, b, c \le 1{,}000{,}000$, and at least one of $a$ and $b$ is non-zero for every thread. No two threads describe exactly the same line.

Unfortunately, Bessie's list of equations also contains some lines that do not actually pass through the interior of the hoop's circle; those lines should simply be ignored.

The origin $(0,0)$ is the exact center of the hoop, so every point on the hoop's edge is at distance $d$ from the origin. Bovine embroidery is admired more when threads cross more often. Count the number of pairs of threads that intersect on the cloth, i.e., at a point within distance $d$ of the origin. If three threads all pass through the same point, that counts as three intersecting pairs; four threads through one point count as six pairs, and so on.

Input

  • Line 1: two space-separated integers $N$ and $d$.
  • Lines 2 to $N+1$: line $i+1$ describes thread $i$ with three integers $a$, $b$, and $c$.

Output

  • A single integer: the number of pairs of threads that intersect within distance $d$ of the origin.

Hint

For example, with $d = 1$, consider the two threads $x = 0$ (the line $1,x + 0,y + 0 = 0$) and $y = 0$ (the line $0,x + 1,y + 0 = 0$). They meet at the origin $(0,0)$, whose distance from the center is $0 \le 1 = d$, so this pair is counted once.