For BROCLK-
I used simple formula.
Suppose we want to calculate cos(nx), then-
Even n-
Just use cos(nx)=2{cos}^{2}(n/2)x-1 . We know cosx and hence we can find numerator and denominator in logN time.
Odd n-
Notice that denominator portion for cos(nx) (unreduced form-numerator and denominator not in lowest term) is nothing but {L}^{n}. Now we use-
Cos(nx)+cos(x)=2*cos((n+1)/2x)*cos((n-1)/2x)
The RHS of denominator is nothing but case of even n, which we did earlier. The only thing to do is to subtract cos(x). Now, notice that cos(x)=D/L. I already know the final denominator, I just need to know the numerator part. Its nothing but D* DenominatorOf(cos((n+1)/2x)*cos((n-1)/2x)) [I think you guys guessed the denominator by now? :3 )
Solution- CodeChef: Practical coding for everyone
For POINPOLY-
LOL this was a rollercoaster for comedy as I solved it. Initially, I was like “MEh, Geometry. GGWP I give up.”
But, just for fun’s sake, I decided to experiment out and submitted a random solution which passed for 70 points. Then my next few hundred submissions were spent on trying to grab those 30 points.
What I did was-
Assume large N. We know that since no 3 points are collinear, there must be difference of at least 1 in y coordinate for any 3 points, else y=k is a line passing through them. This means, if N is large, the diagonally opposite vertex is kind of “far” from the original one. So just take their mid point - it will lie inside the polygon.
Yeah, thats it, no checks on if point is inside or etc. The only thing to know is “how far” you want, or “how far” will hold good.
The principle assumptions of this approach is-
- The polygon is wide enough so that difference of +-0.5 does not affect the answer (in case the (X_1 + X_2) or(Y1+Y2) is odd.
- The polygon is convex, not concave.
The only place it failed was a specific case for N=10, which was narrow. For that I had to include a check that for small polygons actually check if it lies inside or not. Yeah, sad but had to swallow my pride