Broken Clock Brokes my Heart :p

Hello everyone : I solved Broken clock with very intuitive naive approach but i didnt get why it is not working for some subtasks … The approach is discussed below hope u like it.

  1. Calculate angle with
    angle = (ld)((ld)acos ((ld)d/length) * (ld)180.0) / PI;
    //arch cosine for inverse angular calculations.
    ld refers to the long double or an double value valid for both.

  2. As we get initial angle then i iterate through simple loop till the final time is not reached as the angle is fixed i increment the angle with the initial angle calulated above and at every step of iteration if we are in first quad i uses

ans=(ld)((ld)(cos (anglePI/180.0))(ld)length);

//cosine formula simple cos(angle) till now from the base line which is the (complete y axis line * length of the minute arm) (constant) we get the y coord of the end point of the minute arm after a patricular second in an loop.

3.If i am in 2 quadrant

where if(angle > (ld)90.0 && angle<(ld)180.0) {//2 quad

                  ld angle2=angle;angle2-=(ld)90.0;//angle ki copy banayi
                    ans=(ld)((ld)length*(ld)((sin (angle2*PI/180.0))));
                    ans=-ans;
                    // ct1(ans);//exit(0);
                }

As u can see i use sin approach in 2 quad but by taking the angle with respect to the x axis thats why i subtracted total angle with 90 to get new angle angle 2 with respect to the x axis …

when final ans is calulated i put a negative sign

Now similarly in 3 quad

else if(angle>(ld)180.0&&angle<(ld)270.0) {//3 qaud

                    ld angle2=angle;angle2-=(ld)180.0;
                    
                    // angle-=180;
                    ans=(ld)((ld)(cos (angle2*PI/180.0))*(ld)length);
                    ans=-ans;
                }

And in 4 quad

else if(angle>(ld)270.0&&angle<(ld)360.0){//door tak repeat nhi ho payenge vo

                    ld angle2=angle;angle2-=(ld)270.0;
                    
                            ans=(ld)((ld)length*(ld)(sin (angle2*PI/180.0)));
                }

Now if angle exceeds 360 degrees

else if(angle>(ld)360.0) {

                angle-=(ld)360.0;
                // is ubtracted 360 from it to get the new angle from the base line and then 
                applying the cos approach as it is again comes to the 1 quadrant  
            
                ans=(ld)((ld)(cos (angle*PI/180.0))*(ld)length);
                
                
            }  

Let me remember you that the angle which we are calulated is in double so all the ans in every iteration are in double values as i typecasted them in to double in every equation…

Now finally lets say the ans which is calulated after t sec is accurate up to 6 decimal places by using fixed setprecison(9)

4 I uses gcd approach to find the rational form

//ans which we obtained after t seconds

     ll tempnum=(ll)(ans*1000000),tempdeno=1000000;

     ll gco=mygcd(tempnum,tempdeno);

    // ct1(gco);
ll numo=tempnum/gco;
ll deno=tempdeno/gco;

5 And then fianlly
result=mulmod((modInverse(deno,mod)),numo,mod);

a. modinverse by fermats little theorem

b. mulmod for multiplyign the negative or positive large values to ensure no overflow occurs 

c. And then finally taking mod with repect to the mod (1000000007)	 

pLzz refer to my solution below i explained each and every thing in the code below plzz help me at least partial acceptance should be their for my approahch as no tle comes why WA comes any help will be so benefit for me THNKSS FOR ADVANCE…

MY SOLUTION

u r calculating value in double and how will u calculate it to fraction
without any loss of precision

https://www.codechef.com/problems/QTREE

this qtree question

if stuck see this solution

https://www.codechef.com/viewsolution/16227485

1 Like

As @square_root_pi said, you need to calculate the value in fractions and fractions alone, as that is required in the output format. You cannot use any floating point data type as then you will lose precision. Handling all the calculations without using float is the real challenge.
I have written an editorial on how to do that… https://discuss.codechef.com/questions/122855/broclk-editorial-non-matrix-approach-unofficial

2 Likes

@coder_ishmeet even if u calculate upto infinite decimal places ur answer wll be wrong

because float and double itself gives wrong values in decimals

as in double 1.999… is converted to 2,so if we take int of it (1.999…) then it converted to 1,then how check this condition?
plz help…

but i calculated values up to
8 or 6 decimal points with precision
even then the ans will be wrong i didnt understand this as finally if my double values are accurate up to 3 decimal digits or 6 decimal digits after the decimal point the fraction form which i obtained by performing gcd will still be the same then where is the point of loosing precision and where it will affect my ans i’m not clear with that plzz help @paintbrush @taran_1407 ?