HDU 4033 regular polygon (Binary + cosine theorem)

Source: Internet
Author: User
Regular Polygon

Time Limit: 2000/1000 MS (Java/others) memory limit: 65768/65768 K (Java/Others)
Total submission (s): 3274 accepted submission (s): 996


Problem descriptionin A 2_D plane, there is a point strictly in a regular polygon with n sides. if you are given the distances between it and n vertexes of the regular polygon, Can you calculate the length of regularpolygon's side? The distance is defined as dist (a, B) = SQRT (ax-bx) * (ax-bx) + (Ay-by) * (Ay-)). and the distances are given counterclockwise.

 

Inputfirst a integer T (T ≤ 50), indicates the number of test cases. every test case begins with a integer N (3 ≤ n ≤100), which is the number of regular polygon's sides. in the second line are n float numbers, indicate the distance between the point and n vertexes of the regular polygon. all the distances are between (0, 10000), not intrusive.

 

Outputfor the ith case, output one line "case K:" at first. then for every test case, if there is such a regular polygon exist, output the side's length rounded to three digits after the decimal point, otherwise output "impossible ".

 

Sample input233.0 4.0 5.031.0 2.0 3.0

 

Sample outputcase 1: 6.766 Case 2: Impossible

 

Source the 36th ACM/ICPC Asia Regional Chengdu site -- online contest knows the distance between a vertex and N vertices of the positive n edge shape, and obtains the edge length of the positive n edge shape. Idea: in a known expression, the side length of the n edge cannot be obtained. However, the sum of the two sides is greater than the third side, and the difference between the two sides is the third side of the small fish. We can get the range of this edge. then, because the n-edge is connected to all vertices at any point, and the sum of the angles is 360, you only need to take two parts to determine whether the two points are satisfied. Code:
1 # include & lt; cstdio & gt; 2 # include & lt; cstring & gt; 3 # include & lt; cmath & gt; 4 # include & lt; Algorithm & gt; 5 # define PI ACOs (-1.0) 6 # define ESP 1e-8 7 using namespace STD; 8 double AA [105]; 9 int main () 10 {11 int cas, N; 12 double RR, ll; 13 scanf ("% d", & CAS); 14 for (INT I = 1; I <= CAS; I ++) 15 {16 scanf ("% d ", & N); 17 for (Int J = 0; j <n; j ++) 18 scanf ("% lf", AA + J ); 19 // determine the upper and lower boundary 20 LL = 20001, RR = 0; 21 for (Int J = 0; j <n; j ++) 22 {23 RR = max (RR, AA [J] + AA [(j + 1) % N]); 24 LL = min (LL, FABS (AA [J]-aa [(j + 1) % N]); 25} 26 double mid, sum, cosa; 27 printf ("case % d:", I); 28 bool tag = 0; 29 While (RR> ESP + LL) 30 {31 mid = LL + (RR-LL)/2; 32 sum = 0; 33 for (Int J = 0; j <n; j ++) {34 // oosr = A * A + B * B-mid * mid; cosine theorem for angle, then, judge whether the sum of all the angle values is 36035 cosa = (AA [J] * AA [J] + AA [(j + 1) % N] * AA [(j + 1) % N]-mid * mid)/(2.0 * AA [J] * AA [(j + 1) % N]); 36 sum + = ACOs (COSA ); 37} 38 If (FABS (Sum-2 * PI) <ESP) {39 tag = 1; 40 printf ("%. 3lf \ n ", mid); 41 break; 42} 43 else44 if (sum <2 * PI) LL = mid; 45 else46 RR = mid; 47} 48 if (TAG = 0) 49 printf ("impossible \ n"); 50} 51 return 0; 52}
View code

 

HDU 4033 regular polygon (Binary + cosine theorem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.