Lattice Points in the Union of Circles
Time limit1sMemory limit128 MB
Count integer lattice points inside the union of up to 10,000 circles, restricted to the coordinate box from -16383 to 16384.
- Level
Hard8 of 10
- Topics
- Geometry, Sorting, Brute force, Implementation
- Solved
- No attempts yet
Problem
You are given several circles whose center coordinates and radii are all integers. Write a program that outputs the number of points with integer coordinates that are contained in the union of these circles.
For example, in the diagram below the smaller circle (center , radius ) contains points, the larger circle (center , radius ) contains points, and the union of the two circles contains points. A point lying exactly on the boundary of a circle is considered to be contained by that circle.
As an additional constraint, the program must count only points whose and coordinates are integers in the range to , that is, from to inclusive. Circles may extend beyond this region, but points outside the region must not be counted.

Input
The input consists of several data sets. Each data set contains one line per circle. Each line holds three integers separated by spaces: the coordinate of the circle's center, the coordinate, and the radius. The end of a data set is marked by a line of three zeros, 0 0 0. A single data set contains at most 10,000 circles. A data set with no circles (that is, a 0 0 0 line appearing immediately) marks the end of the whole input.
Output
For each data set output Problem #n: followed by a single space and the answer (the number of points contained in the union). Here is the data set number, starting from .