SP3415 SAMER08K - Shrinking Polygons
Description
A polygon is said to be _inscribed_ in a circle when all its vertices lie on that circle. In this problem you will be given a polygon inscribed in a circle, and you must determine the minimum number of vertices that should be removed to transform the given polygon into a _regular polygon_, i.e., a polygon that is equiangular (all angles are congruent) and equilateral (all edges have the same length).
When you remove a vertex _v_ from a polygon you first remove the vertex and the edges connecting it to its adjacent vertices _w_ $ _{1} $ and _w_ $ _{2} $ , and then create a new edge connecting _w_ $ _{1} $ and _w_ $ _{2} $ . Figure (a) below illustrates a polygon inscribed in a circle, with ten vertices, and figure (b) shows a pentagon (regular polygon with five edges) formed by removing five vertices from the polygon in (a).
In this problem, we consider that any polygon must have at least three edges.
Input Format
The input contains several test cases. The first line of a test case contains one integer _N_ indicating the number of vertices of the inscribed polygon (3 N N integers _X_ $ _{i} $ separated by single spaces (1 X $ _{i} $ i N -1). Each _X_ $ _{i} $ represents the length of the arc defined in the inscribing circle, clockwise, by vertex _i_ and vertex (_i_+1) mod _N_. Remember that an _arc_ is a segment of the circumference of a circle; do not mistake it for a _chord_, which is a line segment whose endpoints both lie on a circle.
The end of input is indicated by a line containing only one zero.
Output Format
For each test case in the input, your program must print a single line, containing the minimum number of vertices that must be removed from the given polygon to form a regular polygon. If it is not possible to form a regular polygon, the line must contain only the value -1.