Regular Polygon - UVa 10824 几何暴力

来源:互联网 发布:网络策略 编辑:程序博客网 时间:2024/05/21 07:10

Regular Polygon
Input: 
Standard Input

Output: Standard Output

 

A regular polygon is an n-sided polygon in which the sides are all the same length and are symmetrically placed about a common center (i.e., the polygon is both equiangular and equilateral). Only certain regular polygons are "constructible" using the classical Greek tools of the compass and straightedge. The terms equilateral triangle and square refer to the regular 3- and 4-polygons, respectively.  The picture below shows some regular polygons with different number of sides:

 


Given N(0<N≤2000) points on a particular circle, your job is to find out how many regular polygons of different number of edges are formed by these points. For example if you are given 100 points, then these 100 points will form a regular hexagon if six of these 100 points are the vertices of a regular hexagon.

Input

The input file contains at most 10 sets of inputs. The description of each set is given below. The input file contains at most 20000 lines in total.

The first line of each set is an integer N(0<N≤2000) which indicates how many points are there in this set. Each of the next N lines contains two floating-point numbers, which is the Cartesian coordinate of a point (Accurate to at least nine decimal places). You can assume that all the points lie on the same circle, the center of this circle is the origin, and the coordinate of any two points will not be the same. Also assume that two points are same if their angular distance with respect to the centre of the circle is less than 10-8 radian.

 

Input is terminated by a set where the value of N is zero. This set should not be processed.

 

Output

For each set of input produce one or more lines of output. First line of the output for each set contains the serial of output as shown in the output for sample input. Each of the next few lines will contain two integers S and F, where S denotes the number of sides of a regular polygon and F denotes how many times it is formed by the input points. These outputs should be sorted in ascending order of S. The regular polygons, which are not formed by the input points, should not be reported. For example, in case of the second sample input no regular pentagon (5-gon) is formed, so it is not reported in the output. 

 

 

 

Sample Input                                Output for Sample Input

5
-1000.0000000000 0.0000000000
-500.0000000000 866.0254037844
500.0000000000 866.0254037844
-500.0000000000 -866.0254037844
1000.0000000000 0.0000000000
6
-800.0000000000 0.0000000000
800.0000000000 0.0000000000
-400.0000000000 692.8203230276
400.0000000000 692.8203230276
400.0000000000 -692.8203230276
-400.0000000000 -692.8203230276

0

Case 1:
3 1
Case 2:
3 2

6 1



题意:求正k边形的个数。

思路:暴力枚举k,判断是否存在符合的点,访问过的点不用再访问了。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>using namespace std;const double eps = 1e-8;struct node{ double x,y,atan;}dian[2010];bool cmp(node a,node b){ return a.atan<b.atan;}int n;long long ans[2010];double ang[2010],angle[2010],R,R2,vis[2010],pi=3.14159265358979;int solve(double to){ int l=1,r=n,mi;  while(l<r)  { mi=(l+r)/2;    if(dian[mi].atan>to-eps)     r=mi;    else     l=mi+1;  }   return l;}int main(){ int i,j,k,p,num,t=0,T=0,pos;  double m,start,to;  while(~scanf("%d",&n) && n)  { memset(ans,0,sizeof(ans));    for(i=1;i<=n;i++)    { scanf("%lf%lf",&dian[i].x,&dian[i].y);      dian[i].atan=atan2(dian[i].y,dian[i].x)+pi;    }    sort(dian+1,dian+1+n,cmp);    for(k=3;k<=n;k++)    { m=2*pi/k;num=0;      T++;      for(i=1;i<=n;i++)       if(vis[i]!=T)       { start=dian[i].atan;         vis[i]=1;         for(j=1;j<k;j++)         { to=start+j*m;           if(to>2*pi+eps)            to-=2*pi;           pos=solve(to);           while(pos<=n && vis[pos]==T && to>dian[pos].atan-eps && to<dian[pos].atan+eps)            pos++;           if(pos>n)            pos-=n;           while(pos<=n && vis[pos]==T && to>dian[pos].atan-eps && to<dian[pos].atan+eps)            pos++;           if(to>dian[pos].atan-eps && to<dian[pos].atan+eps && vis[pos]!=T)             vis[pos]=T;           else             break;         }         if(j==k)          ans[k]++;       }    }    printf("Case %d:\n",++t);    for(k=3;k<=n;k++)     if(ans[k]>0)      printf("%d %lld\n",k,ans[k]);  }}



0 0
原创粉丝点击