PROBLEM LINK:Author: Trung Nguyen DIFFICULTY:MEDIUM PREREQUISITES:Trigonometry, Fast Matrix Exponentiation, Modular Inverse PROBLEM:A clock has a minute hand which goes $x$ angle per second. The minute hand has length $l$ and the center of clock has coordinate $(0,0)$. Initially, the other endpoint is at coordinate $(0,l)$, and after $1$ second its $y$coordinate becomes $d$. Given $l,d,t$($x$ is unknown), calculate the $y$coordinate of this endpoint after $t$ seconds. QUICK EXPLANATION:By the problem statement we have $\cos(x)=d/l$, and the final answer is $\cos(tx)\cdot l$. So we only need to compute $\cos(tx)$. There is a formula computing $\cos(nx)$: $$\cos(nx)=2\cos(x)\cos((n1)x)\cos((n2)x),$$ and we can use fast matrix exponentiation to compute $\cos(tx)$ in $O(\log t)$ time. EXPLANATION:A note to modular arithmeticSetter's SolutionFirst notice that $x$ is given by $\cos(x)=d/l$. To see this, consider the following figure which illustrates the problem statement: $OA$ and $OB$ are the position of minute hand initially and after $1$ seconds, respectively. And the answer after $t$ seconds is $\cos(tx)\cdot l$. Therefore, our actual task is: given $\cos(x)$ and integer $t$, compute $\cos(tx)$. Notice the following formula: $$\cos(nx)=2\cos(x)\cos((n1)x)\cos((n2)x)\ \ \ (*)$$ that means we can write $\cos(tx)$ in a matrixmultiplication form: $$\begin{pmatrix}\cos(tx)\\\cos((t1)x)\end{pmatrix}=\begin{pmatrix}2\cos(x)&1\\1&0\end{pmatrix}\begin{pmatrix}\cos((t1)x)\\\cos((t2)x)\end{pmatrix},$$ and therefore $$\begin{pmatrix}\cos(tx)\\\cos((t1)x)\end{pmatrix}=\begin{pmatrix}2\cos(x)&1\\1&0\end{pmatrix}^{t1}\begin{pmatrix}\cos(x)\\1\end{pmatrix}.$$ Thus we can compute $\cos(tx)$ by fast matrix exponentiation in $O(\log t)$ time. Tester's SolutionAgain, we're given $\cos(x)$ and integer $t$ and we want to compute $\cos(tx)$. But what if we don't know formula $(*)$? We can use the following (betterknown) formula: $$\begin{align*} \cos(a+b)=&\cos(a)\cos(b)\sin(a)\sin(b)\ \ \ (1)\\ \sin(a+b)=&\sin(a)\cos(b)+\cos(a)\sin(b)\ \ \ (2) \end{align*}$$ All numbers we come across will be in the form $a+b\sin(x)$. Although we don't know $\sin(x)$, we can represent such a number as $(a,b)$. Therefore we can define $+,,\times$ on these numbers: $$\begin{align*} (a,b)\pm(c,d)=&(a\pm c,b\pm d)\\ (a,b)\cdot(c,d)=&(a+b\sin(x))(c+d\sin(x))\\ =&ac+bd(1\cos^2(x))+(ad+bc)\sin(x)\\ =&(ac+bd(1\cos^2(x)),ad+bc). \end{align*}$$ We can write a recursive procedure $\mathrm{Solve}(t)$ for computing the pair $(\cos(tx),\sin(tx))$.
This procedure runs in $O(\log t)$ time since every two steps decrease $t$ by half. Suppose we've computed $\cos(tx)=(a,b)$. $b$ must be equal to $0$ since it's possible to represent $\cos(tx)$ by only $\cos(x)$ terms. Therefore $\cos(tx)=a$ and we're done. On formula $(*)$How to prove formula $(*)$? In fact I don't know, but one might look into this page for answer. The Chebyshev polynomial of the first kind is defined as $T_0(x)=1$, $T_1(x)=x$ and $T_{n+1}(x)=2xT_n(x)T_{n1}(x)$. It can be proved that $T_n(\cos(x))=\cos(nx)$, which directly implies formula $(*)$. ALTERNATIVE SOLUTION:Please feel free to share your approach :) AUTHOR'S AND TESTER'S SOLUTIONS:Author's solution can be found here. RELATED PROBLEMS:
This question is marked "community wiki".
asked 27 Jan, 14:05

I calculated cosnx by breaking n into a sum of powers of 2 and then calculated each power recursively using the formula cos2x=2cos^21. I used memoization to avoid repeating the same calculations. This required no more than log(n) operations. To combine the powers of 2, I used the formula cos(a+b)=2cos(a)cos(b)cos(ab). I also used memoisation in this approach by mapping the previously calculated values of cosx otherwise it would result in TLE. answered 13 Feb, 04:00

For formula proof: answered 12 Feb, 17:46

Really good problem... never used matrix exponentiation like fibonacci's anywhere else before answered 13 Feb, 01:28

Wow, I found (another) simple proof for the formula in @taran_1407's editorial! Link. answered 12 Feb, 17:50

Can someone help me understand the formula manipulation to get from the first matrix representation of the formula to the second one with the second 2x2 matrix raised to the (t1) power? answered 13 Feb, 03:31
1
It is written in French but I guess you should be able to understand the basic idea by reading the equation. So look for chapter: "Suite récurrente d’ordre p" on page: https://fr.wikipedia.org/wiki/Suite_r%C3%A9currente_lin%C3%A9aire NB: I did not find the same explanation in English.
(13 Feb, 14:19)
It just follows from plain matrix multiplication. Any linear recurrence can solved in this manner using matrix exponentiation. You can find relevant tutorials on the internet.
(13 Feb, 21:43)
Okay thanks for the replies. I'm actually relatively new to competitive programming and have done 0 matrix exponentiation problems so far. I think I just need to learn the topic and then get some practice with it.
(17 Apr, 22:49)

need some help!
then i found the angle with respect to y axis after t seconds as this
now I found the quadrant where the final angle will lie and calculate the value of d(the final y coordinate) the i converted d into fraction using the function and just find the inverse and print it where i am wrong my solution my solution for BROCLK
link
This answer is marked "community wiki".
answered 13 Feb, 07:12

came up with the exact formula of author . but didn't have prior knowledge of Mexp.. it was a very good question ..
What about simply using the 2 formulas
$cos (tx)= 2*{cos}^{2}(tx/2)1$ (even t)
and $cos(tx)+cos(x)=2cos((t+1)x/2)*cos((t1)x/2)$ (odd t)
No need for any complex as such formula if we use these.
yes.. this will be much time saver and also no dealing with irrational parts.