Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

Ferrarixxx's blog

By Ferrarixxx, history, 6 weeks ago, In English,

Hi Guys,
The above picture shows my approach for this problem https://codeforces.com/contest/1354/problem/C1

Am basically using the interior angle subtended by 3 adjacent vertices and then to find the vertical ( perpendicular ) line's length using tan function
But am getting completely unexpected answer.

// -- MY CODE :


long double n;

cin >> n;

// long double angle = (((180 * (2*n — 2)) / (2*n) ) / 360) * 2 * 3.14

long double angle = ((((n — 1)) / n)) * 3.14;

cout << tan(angle) << endl;


PLEASE GUYS HELP ME FIND MY MISTAKE .
THANKS IN ADVANCE.

 
 
 
 
  • Vote: I like it
  • +6
  • Vote: I do not like it

»
6 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

The angle(theta) is half of the interior angle, divide the theta by two and also take pi upto some more decimals to prevent WAs.
I meant in your code...it should be like this.

long double angle = (n - 1) / (2 * n) * 3.14159265359;
  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have done it like u said but still getting unexpected ans

    Code Snippet:

    #define pi 3.14159265358979323846;

    long double n;

    cin >> n;

    // long double angle = (((180 * (2*n — 2)) / (2*n)) / 360) * 2 * 3.14;

    long double angle = (((n — 1)) / n) * pi;

    cout << tan(angle) << endl;

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      You didn't see the change I did? I said to divide the angle(theta) by 2 in your code.
      In your code:

      long double angle = (((n — 1)) / n) * pi;
      

      It should be:

      long double angle = (n - 1) / (2 * n) * pi;
      
  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    For maximum precision you can use:

    long double PI = acos(-1);
    
»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Hi Guys,
Thanks for the help , i finally solved it .

Am facing another new Problem in C2 (https://codeforces.com/contest/1354/problem/C2)
(This is the last one i promise)
-

In C2 problem this is my simple approach
and again am not able to identify why am getting unexpected output
(Is my approach incorrect?)

Code Snippet :

long double pi = 3.14159265358979323846;

 long double n;
 cin >> n;

 long double theta = (2 * (pi)  ) / 2 * n;

 // long double ans = sqrt( 1 / (2 * (1 - cos(theta))) );

 long double ans = sqrtf( 1 / (2 * (1 - cos(theta))) );

 cout << setprecision(20) << ans << endl;

Please Help me out.

Thanks In Advance. :)

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I don't think what you're doing is correct.
    I may be wrong. But I did something different.
    I think the ideal arrangement should be like this(my drawing's not too good)
    Whats-App-Image-2020-05-29-at-9-40-01-PM
    Here's my code if you want to refer 80726717

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    My above illustrated approach for the problem https://codeforces.com/contest/1354/problem/C2 is correct :

    long double pi = 3.14159265358979323846;
    
        long double n;
        cin >> n;
    
        long double theta = (2 * (pi)  ) / (2 * n);
    
        // long double ans = sqrt( 1 / (2 * (1 - cos(theta))) );
    
        long double ans = 2 * (long double)sqrtf( 1 / (2 * (1 - (long double)cos(theta))) );
    
        cout << setprecision(20) << ans << endl;

    This code is working fine but it lacks precision. i.e for this testcase :
    3
    3
    5
    199

    These are the actual answers :
    1.931851653
    3.196226611
    126.687663595

    but my code outputs this :
    2
    3.2360680103302001953
    126.68865203857421875

    IF ANYONE CAN HELP ME HERE THEN PLEASE TYPE A COMMENT

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Can Anyone help me out here to increase precision of my above code ??

      If its not possible then atleast type a comment 'not possible', then i'll stop asking.

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Maybe change pi to be = acos(-1);

        It's just c++, your answer is correct.