Mod by powers of 2 In my computer science class I had to solve the following problem from Codeforces:
https://codeforces.com/problemset/problem/55/A
It's about some pillows placed on a circle and a fly that jumps over $n$ pillows, where $n = \overline{0,k}$. The fly does this for an infinite amount of time. Will the fly reach all the pillows for a certain number of pillows $z$?
By playing around I had discovered that the fly can reach all pillows when the initial number of pillows $z$ is a power of 2 and the program was solved, but I want to prove it mathematically. I managed to describe the movements of the fly by the following sequence: $a_n = (a_{n-1} + n)mod\ z, a_0 = 1;$ Doing some internet research I found out that I can write $a_n = (a_{n-1} + n)$ as this explicit function $\frac{n(n+1)}{2} + 1$. This is where I am stuck. Is there any method to prove this in a digestible way(for a 1st year Computer Science student)?
 A: Continuing your line of thought, the question boils down to asking whether every remainder mod $z$ is of the form $\tfrac{n(n+1)}{2}+1$ for some nonnegative integer $n$. Note that if $n\equiv m\pmod{z}$ then also
$$\frac{n(n+1)}{2}+1\equiv\frac{m(m+1)}{2}+1,$$
so if a remainder is of the form $\tfrac{n(n+1)}{2}+1$ for some nonnegative integer $n$, then it is also of the form $\tfrac{n(n+1)}{2}+1$ for some nonnegative integer $m<z$. This shows that the fly reaches every pillow if and only if the map
$$\Bbb{Z}/z\Bbb{Z}\ \longrightarrow\ \Bbb{Z}/z\Bbb{Z}:\ n\ \longmapsto\ \frac{n(n+1)}{2}+1,$$
is surjective. Because $\Bbb{Z}/z\Bbb{Z}$ is finite this happens if and only if the map is injective. Can you figure out for which $z$ the map is injective?
By the way, some caution should be taken in writing $\tfrac{n(n+1)}{2}+1$ in $\Bbb{Z}/z\Bbb{Z}$. If $z$ is even then it is not clear what dividing by $2$ should mean. In the above, just remember that we got this expression from
$$\tfrac{n(n+1)}{2}=1+2+3+\ldots+n.$$

Without having to figure out exactly for which values of $z$ this map is surjective, this idea already helps in writing your program; it shows that if the fly eventually reaches all pillows, then it reaches all pillows within $z$ minutes. So you only need to check the position of the fly in the first $z$ minutes; if it visits the same pillow twice, you're done. If it doesn't, then it visits every pillow.
