Bovine Embroidery

Time limit1sMemory limit128 MB

Summary
Given N lines and a circle of radius d, count pairs of chords whose intersection point lies within distance d of the origin; lines missing the circle are ignored.
Level

Medium7 of 10

Topics
Geometry, Sorting, Math, Implementation
Solved
No attempts yet

Problem

Bessie has taken up the detailed art of bovine embroidery. Cows embroider a cloth mounted in a circular hoop of integer radius dd (1≤d≤50,0001 \le d \le 50{,}000). They sew NN (2≤N≤50,0002 \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=0ax + by + c = 0. The coefficients aa, bb, and cc are integers with −1,000,000≤a,b,c≤1,000,000-1{,}000{,}000 \le a, b, c \le 1{,}000{,}000, and at least one of aa and bb 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)(0,0) is the exact center of the hoop, so every point on the hoop's edge is at distance dd 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 dd 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 NN and dd.
  • Lines 2 to N+1N+1: line i+1i+1 describes thread ii with three integers aa, bb, and cc.

Output

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

Hint

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

Examples3

  1. Example 1

    Input
    2 1
    1 0 0
    0 1 0
    
    Expected output
    1
    
  2. Example 2

    Input
    2 5
    1 0 -1
    1 0 -2
    
    Expected output
    0
    
  3. Example 3

    Input
    3 10
    1 0 0
    0 1 0
    1 -1 0
    
    Expected output
    3