UVa 10885 - Martin the Gardener

10885 - Martin the Gardener

 * http://acm.uva.es/p/v108/10885.html

Summary
Find a set of 13 points on a 2-dimensional Cartesian plane, such that:
 * the points have integer coordinates
 * the distance between any two points is an integer
 * no three points lie on the same straight line.

Explanation
We can weaken the first two conditions. Suppose, that we have found a set of 13 points with rational coordinates and rational distances between every two of them. Let L be the LCM of denominators of all coordinates and distances. Then, by multiplying coordinates of every point by L, we'll get a set of points with integer coordiantes and integer distance between every two of them.

Now, let's impose an additional constraint on the set of points. We'll be looking for a set of points, all of which lie on the same circle. It's easy to see, that every three points from this set will not lie on the same line, because a straight line intersects a circle in at most two points.

As a simplification, we may further assume that the points lie on a circle of unit radius, whose center is at the origin. Then the coordinates of the points may be written in the following form:

$$x_i = \cos \alpha_i$$, $$y_i = \sin \alpha_i$$, for i=1..13.

The distance between i-th and j-th points is: $$ \sqrt{(\cos\alpha_i - \cos\alpha_j)^2 + (\sin\alpha_i - \sin\alpha_j)^2} = \sqrt{2 - 2 \cos(\alpha_i - \alpha_j)} = 2 \sqrt{\frac{1 - \cos(\alpha_i - \alpha_j}{2})} = 2 \sin(\frac{\alpha_i - \alpha_j}{2}) $$

If we add the constraint, that every $$\sin\frac{\alpha_i}{2}$$ and $$\cos\frac{\alpha_i}{2}$$ are rational, then the points will have rational coordiantes, and distance between any two of them will be rational, too.

Let $$\sin(\frac{\alpha_i}{2}) = \frac{p_i}{q_i}$$, for some integers $$p_i$$ and $$q_i$$.

Then $$\cos(\frac{\alpha_i}{2}) = \frac{\sqrt{q_i^2 - p_i^2}}{q_i}$$. This value will be rational if $$q_i^2-p_i^2=r_i^2$$, for some integer $$r_i$$.

So, what we are now really looking for if is just a set of pythagorean triples $$(r_i,p_i,q_i)$$, that is integers, which satisfy the equation: $$r_i^2+p_i^2=q_i^2$$.

Having found a few of them (by e.g. brute force enumeration), you can express the coordinates of points:

$$x_i = \frac{r_i^2 - p_i^2}{q_i^2}$$, $$y_i = \frac{2 p_i r_i}{q_i^2}$$.

Output
0 180 0 3876 432 0 432 4056 1615 0 1615 4056 2047 180 2047 3876 2511 3528 2511 528 2880 1020 2880 3036 3136 2028