Let $A_1 A_2 A_3 A_4 A_5 A_6 A_7 A_8$ be a regular octagon.  Let $M_1$, $M_3$, $M_5$, and $M_7$ be the midpoints of sides $\overline{A_1 A_2}$, $\overline{A_3 A_4}$, $\overline{A_5 A_6}$, and $\overline{A_7 A_8}$, respectively.  For $i = 1, 3, 5, 7$, ray $R_i$ is constructed from $M_i$ towards the interior of the octagon such that $R_1 \perp R_3$, $R_3 \perp R_5$, $R_5 \perp R_7$, and $R_7 \perp R_1$.  Pairs of rays $R_1$ and $R_3$, $R_3$ and $R_5$, $R_5$ and $R_7$, and $R_7$ and $R_1$ meet at $B_1$, $B_3$, $B_5$, $B_7$ respectively.  If $B_1 B_3 = A_1 A_2$, then $\cos 2 \angle A_3 M_3 B_1$ can be written in the form $m - \sqrt{n}$, where $m$ and $n$ are positive integers.  Find $m + n$.
We use coordinates. Let the octagon have side length $2$ and center $(0, 0)$. Then all of its vertices have the form $(\pm 1, \pm\left(1+\sqrt{2}\right))$ or $(\pm\left(1+\sqrt{2}\right), \pm 1)$.
By symmetry, $B_{1}B_{3}B_{5}B_{7}$ is a square. Thus lines $\overleftrightarrow{B_{1}B_{3}}$ and $\overleftrightarrow{B_{5}B_{7}}$ are parallel, and its side length is the distance between these two lines. However, this is given to be the side length of the octagon, or $2$.
Suppose the common slope of the lines is $m$ and let $m=\tan\theta$. Then, we want to find \[\cos 2\left(90-\theta\right)=2\cos^{2}\left(90-\theta\right)-1=2\sin^{2}\theta-1.\]
It can easily be seen that the equations of the lines are \begin{align*} B_{1}B_{3}: y-mx+m\left(1+\sqrt{2}\right)=0 \\ B_{5}B_{7}: y-mx-m\left(1+\sqrt{2}\right)=0.\end{align*} By the [distance between parallel lines formula](https://artofproblemsolving.comhttps://www.cuemath.com/geometry/distance-between-two-lines/), a corollary of the [point to line distance formula](https://artofproblemsolving.comhttps://brilliant.org/wiki/dot-product-distance-between-point-and-a-line/), the distance between these two lines is \[\frac{|c_{2}-c_{1}|}{\sqrt{a^{2}+b^{2}}}=\frac{2m\left(1+\sqrt{2}\right)}{\sqrt{m^{2}+1}}.\] Since we want this to equal $2$, we have \begin{align*}\frac{2m\left(1+\sqrt{2}\right)}{\sqrt{m^{2}+1}}&=2 \\ 4m^{2}\left(3+2\sqrt{2}\right)&=4m^{2}+4 \\ \left(12+8\sqrt{2}\right)m^{2}&=4m^{2}+4 \\ \left(8+8\sqrt{2}\right)m^{2}&=4 \\ m^{2}&=\frac{4}{8+8\sqrt{2}} \\ \Rightarrow m^{2}=\tan^{2}\theta=\frac{\sin^{2}\theta}{\cos^{2}\theta}&=\frac{1}{2+2\sqrt{2}}.\end{align*} Since $\sin^{2}\theta+\cos^{2}\theta=1,$ we have $\sin^{2}\theta=\frac{1}{3+2\sqrt{2}}$. Thus \[2\sin^{2}\theta-1=\frac{2}{3+2\sqrt{2}}-1=\frac{-1-2\sqrt{2}}{3+2\sqrt{2}}=\frac{\left(-1-2\sqrt{2}\right)\left(3-2\sqrt{2}\right)}{\left(3+2\sqrt{2}\right)\left(3-2\sqrt{2}\right)}=\frac{5-4\sqrt{2}}{1}=5-\sqrt{32}.\] The answer is $\boxed{037}$.