Bovine Embroidery
Time limit1sMemory limit128 MB
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 (). They sew () 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 . The coefficients , , and are integers with , and at least one of and 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 is the exact center of the hoop, so every point on the hoop's edge is at distance 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 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 and .
- Lines 2 to : line describes thread with three integers , , and .
Output
- A single integer: the number of pairs of threads that intersect within distance of the origin.
Hint
For example, with , consider the two threads (the line ) and (the line ). They meet at the origin , whose distance from the center is , so this pair is counted once.