Linear difference equation with modulo operation

71 Views Asked by At

try to solve closed form of recursive equation

$S_{n+1}=\frac{3S_n+(S_n \% 2)}{2}+4$ , $S_1=4$

where "%" is modulo operation, $x\%2$ return $1$ if $x$ is odd else $0$.

This equation is derived from a simple game:

n people with id 0,1,2,...,n-1 sit around a circular table in clockwise, 
start a enumeration from man with id 0, they leave the table when enumerated
as multiple of 3 (id 2 always leave first).
find the last man leaving.

for example $a_4=0$, because 2 leave first, and then 1, 3.

solution of it is $a_n=(a_{n-1}+3)\%n$

Series $a_n$ would be like figure 1,

Growing with slope 3, bounded by y=x, drop every time it hit y=x.

Drop points gives a way to calculate $a_n$ with complexity $O(log\;n)$.

And I tried to reduce it to $O(1)$ by finding closed form of drop point $D_i$

$a_3=1, a_4=0$, let 4 be the first drop point, $D_1=4$.

then $S_n=\sum_{i=1}^{n}D_i$ has form I showed.