RightTriangle
Source: TopCoder SRM 473, RightTriangle.triangleCount (https://archive.topcoder.com/SRM/ProblemStatement/pm/10976/index.html).
places equally spaced locations are numbered 0 to places-1 clockwise around a circle. Generate points red points for n = 0, 1, ..., points-1:
- Compute
P[n] = (a*n*n + b*n + c) mod placesusing exact integer arithmetic. - Starting at
P[n], find the first place in the clockwise direction that does not contain a red point. - Put a red point onto that place.
After all points are placed, return the number of right triangles whose three vertices are red points. A right triangle has non-zero area and exactly one 90-degree angle. For any valid input the answer fits in a signed 64-bit integer.
Diameter rule (circle of equally spaced places): with even places, place p and q are diametrically opposite when (q - p + places) mod places == places / 2. A right triangle occurs iff it uses one diameter pair of occupied points and any third occupied point. Therefore count diameter pairs among occupied points and multiply by (points - 2) when applicable. Do not use an all-places populated closed form unless every place is actually occupied.
Collision-aware clockwise allocation is required. Circular wrap applies.
RightTriangle
Source: TopCoder SRM 473, `RightTriangle.triangleCount` (https://archive.topcoder.com/SRM/ProblemStatement/pm/10976/index.html).