1. 程式人生 > >Codeforces 1 C. Ancient Berland Circus-幾何數學題+浮點數求gcd ( Codeforces Beta Round #1)

Codeforces 1 C. Ancient Berland Circus-幾何數學題+浮點數求gcd ( Codeforces Beta Round #1)

C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input standard input output standard output

Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different.

In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges.

Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time.

You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.

Input

The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.

Output

Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.

Examples input Copy
0.000000 0.000000
1.000000 1.000000
0.000000 1.000000
output Copy
1.00000000

 

 

 

幾何數學題,寫這個題完全就是記憶一下數學公式。。。

 

題意就是給你一個正多邊形三個點的座標,三點一定能構成三角形,通過這三個點求所在的多邊形的最小面積,肯定是邊越少面積越小。

通過三點座標可以求出三角形的三邊長a,b,c;然後求出三角形的面積,然後求出圓的半徑。

然後求出三個角(不是三角形的三個角,是圓心對應的三個角),首先算出餘弦,cosC=(a*a+b*b-c*c)/(2*a*b);就是cosC=(r*r+r*r-c*c)/(2*r*r);

然後反餘弦函式arccos,程式碼就是acos(cosC),求出角度C。

求出來三個角之後,找三個角的最大公約數,然後求出這最大公約數的三角形對應的三角形的面積,然後總的圓心角2*PI/最大公約數,就是一共的個數,乘起來就可以了。

三角形的面積:海倫公式 s=sqrt(p*(p-a)*(p-b)*(p-c)),p=(a+b+c)/2;

又因為三角形面積s=1/2*a*b*sinC.

因為a/sinA=b/sinB=c/sinC=2*r,所以sinC=c/(2*r),所以s=a*b*c/(4*r).

好多啊,不想寫了。。。直接貼一下別人的題解。

傳送門:

CodeForces-1C-Ancient Berland Circus

這個題eps開的太小會錯,一開始寫的1e-9,wa了,改成1e-4過了,精確度太高過不了。。。

 

程式碼:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1e5+10;
 5 const double PI=acos(-1.0);
 6 const double eps=1e-4;
 7 
 8 struct node{
 9     double x,y;
10 };
11 
12 double len(node a,node b)//求邊長
13 {
14     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
15 }
16 
17 double acos(double a,double b,double c)//求反餘弦
18 {
19     return acos((a*a+b*b-c*c)/(2*a*b));
20 }
21 
22 double fgcd(double a,double b)//求浮點數的gcd
23 {
24     if(b<eps) return a;
25     return fgcd(b,fmod(a,b));
26 }
27 
28 int main()
29 {
30     node A,B,C;
31     cin>>A.x>>A.y;cin>>B.x>>B.y;cin>>C.x>>C.y;
32     double a=len(B,C),b=len(A,C),c=len(A,B);
33     //cout<<a<<" "<<b<<" "<<c<<endl;
34     double p=(a+b+c)/2;
35     double s=sqrt(p*(p-a)*(p-b)*(p-c));//海倫公式求三角形面積
36     double r=a*b*c/(4*s);//求圓的半徑
37     double AA=acos(r,r,a),BB=acos(r,r,b),CC=2*PI-AA-BB;//求角度
38     //cout<<AA<<" "<<BB<<" "<<CC<<endl;
39     double angle=fgcd(AA,fgcd(BB,CC));//求最大公約數的角度
40     //cout<<angle<<endl;
41     double sr=(r*r*sin(angle))/2;//求每一小塊的面積
42     double ans=sr*2*PI/angle;
43     printf("%.6f\n",ans);
44 }