codforces 1C Ancient Berland Circus(几何)

题意
给出正多边形上三个点的坐标,求正多边形的最小面积
分析
先用三边长求出外接圆半径(海伦公式),再求出三边长对应的角度,再求出三个角度的gcd,最后答案即为\(S*2π/gcd\),S为gcd对应的三角形的面积
注意如果三个点在同一段半圆弧上,需要thec=2*pi-thea-theb,而不能直接用acos()函数求
数据卡精度,gcd要取0.001才行,其他不行

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
const double pi = acos(-1.0);
double x1,x2,x3,y1,y2,y3;
double calc(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double work(double r,double x)
{
    return (r*r*2-x*x)/(2*r*r);
}
double cal(double a,double b)
{
    while(a-b>=-0.001) a-=b;return a;
}
double gcd(double a,double b)
{
    if(b<=0.001) return a;
    else return gcd(b,cal(a,b));
}
bool feq (double a, double b) {  
    return fabs(a-b) < 0.0001;  
}  
  
double fgcd(double a, double b) {  
    if (feq(a, 0)) return b;  
    if (feq(b, 0)) return a;  
    return fgcd(b, fmod(a, b));  
}  
int main()
{
    cin>>x1>>y1>>x2>>y2>>x3>>y3;
    double a=calc(x1,y1,x2,y2),b=calc(x1,y1,x3,y3),c=calc(x2,y2,x3,y3);
    //printf("a=%f b=%f c=%f\n",a,b,c );
    double p=(a+b+c)/2,S=sqrt(p*(p-a)*(p-b)*(p-c)),r=a*b*c/(4*S);
    double cosa=work(r,a),cosb=work(r,b),cosc=work(r,c);
    //printf("%f %f %f\n",cosa,cosb,cosc );
    double thea=acos(cosa),theb=acos(cosb),thec=2*pi-thea-theb;//注意
    //printf("%f %f %f\n", thea/(2*pi)*180,theb/(2*pi)*180,thec/(2*pi)*180);
    double jiao=gcd(thea,gcd(theb,thec));
    //printf("jiao=%f cnt=%f sin==%f\n",jiao/(2*pi)*180,cnt,sinn);
    double ans=r*r*sin(jiao)/2*2*pi/jiao;
    printf("%.6f\n", ans);
    return 0;
}
posted @ 2017-08-04 15:53  遗风忘语  阅读(194)  评论(0编辑  收藏  举报