<s>[INST] Closed form for the sum $\sum_{k=0}^n a^k \left\lfloor\frac{k}{p}\right\rfloor$ I want a closed form for the sum $$S=\sum_{k=0}^n a^k \left\lfloor\frac{k}{p}\right\rfloor$$
where: $a\ne 1<p<n;\quad p\in\mathbb Z$
I know a related identity, 
$$\quad\displaystyle \sum_{k=0}^n\left\lfloor\frac{k}{p}\right\rfloor=\left(n+1-\frac{p}{2}\right)\left\lfloor\frac{n}{p}\right\rfloor-\frac{p}{2}\left\lfloor\frac{n}{p}\right\rfloor^2\quad\;,$$ 
but I can't apply it to this problem!
Result for this sum: 
$$\quad\displaystyle S=\dfrac{a^{n+1}}{a-1}\left\lfloor\frac{n+1}{p}\right\rfloor-\dfrac{a^p\left(a^{p\left\lfloor\frac{n+1}{p}\right\rfloor}-1\right)}{(a-1)(a^p-1)}$$
Can you prove it?
 [/INST] Write $n=cp+r$, with $c,r\in\mathbb N\cup\{0\}$, and $r<p$. Then, for $k\leq n$,
$$
\left\lfloor\frac kp\right\rfloor=\begin{cases}0,&\ 0\leq k<p\\ 1,&\ p\leq k<2p,\\
\vdots\\
m,&\ mp\leq k<(m+1)p\\
\vdots\\
c,&\ cp\leq k\leq n
\end{cases}
$$
Then
$$
S=\sum_{k=0}^na^k\,\left\lfloor\frac kp\right\rfloor=\sum_{m=0}^{c-1}\sum_{k=mp}^{(m+1)p-1}ma^k\,+\sum_{k=cp}^{n}ca^k
=\sum_{m=1}^{c-1}m\,\sum_{k=mp}^{(m+1)p-1}a^k\,+\sum_{k=cp}^{n}ca^k\\
=\sum_{m=1}^{c-1}m\,\frac{a^{mp}-a^{(m+1)p}}{1-a}+c\,\frac{a^{cp}-a^{n+1}}{1-a}.
$$
Note that in the first sum, if we put together the negative term for index $m$ and the positive for $m+1$, we get 
$$
-ma^{(m+1)p}+(m+1)a^{(m+1)p}=a^{(m+1)p}.
$$
So 
$$
(1-a)S=\sum_{m=1}^ca^{mp}-ca^{n+1}=\frac{a^p-a^{(c+1)p}}{1-a^p}-ca^{n+1},
$$
or
$$
S=\frac{ca^{n+1}}{a-1}-\frac{a^{(c+1)p}-a^p}{(a^p-1)(a-1)}=\frac{ca^{n+1}}{a-1}-\frac{a^p(a^{pc}-1)}{(a^p-1)(a-1)}.
$$
Where 
$$
c=\left\lfloor \frac np\right\rfloor.
$$
</s>