Describe all such functions $f:\mathbb{Z}\to\mathbb{Z}$ that $\forall x, y\ f(x+y+f(y))=f(x)+ny$ The following problem is from the 2012 China Team Selection Test.
Statement. The positive integer $n$ is given. The problem is to find all such functions $f:\mathbb{Z}\to\mathbb{Z}$, that for all $x,y\in\mathbb{Z}$ this equality stands: $ f(x+y+f(y))=f(x)+ny$.
My attemts: I derived that $f(0)=0$. First I took $x=0,y=0$ and get $$f(f(0))=f(0).$$
Suppose $f(0)=a\in\mathbb{Z}$. Then for $y=0$ we have $$f(x+a)=f(x).$$
If we take here $x=a$, we get $f(2a)=a$. Now if we take $x=0,y=a$ we get
$$f(a+f(a))=f(0)+na,$$
wich means that $a = a+na$, $na=0$ and so $a=0$.
I also got that $f(f(-x))=f(x)-nx$ by taking $x=-y$.
What should I do next?
 A: It is convenient to define $g(x)=f(x)+x$; the functional equation then reads $$g(x+g(y))=g(x)+g(y)+ny.$$  We have $g(0)=0$, and setting $x=0$ we have $$g(g(y))=g(y)+ny.$$  Plugging this into the general functional equation, we get $$g(x+g(y))=g(x)+g(g(y)).$$  Letting $z=g(y)$, this says that $g(x+z)=g(x)+g(z)$ as long as $z$ is in the image of $g$.  Let $A$ be the subgroup of $\mathbb{Z}$ generated by the image of $g$; it follows by an easy induction that $g(x+z)=g(x)+g(z)$ is valid whenever $x,z\in A$.  That is, the restriction of $g$ to $A$ is a homomorphism $A\to A$.  Note also that $g$ is not identically zero (since $n>0$), so $A\neq\{0\}$; let $m>0$ be such that $A=m\mathbb{Z}$.  Then we must have $g(z)=qz$ for all $z\in A$, where $q=g(m)/m$ is an integer.  Plugging this into the original functional equation (assuming $x,y\in A$) and equating the coefficients of $y$, we get $$q^2-q-n=0.$$
This has an integer solution iff $4n+1$ is a square, namely $q=\frac{1\pm\sqrt{4n+1}}{2}$.  Thus no such $g$ can exist unless $4n+1$ is a square.
Now consider $g(g(g(y)))$, recalling the earlier identity $g(g(y))=g(y)+ny$.  On the one hand, we have $$g(g(g(y)))=qg(g(y))=qg(y)+qny.$$
On the other hand, we have $$g(g(g(y)))=g(g(y))+ng(y)=qg(y)+ng(y).$$
Comparing these two equations, we find that $g(y)=qy$, for arbitrary $y$.
Summing up, we have shown that there is such a function $g$ iff $4n+1$ is a square, in which case there are exactly two such $g$, namely $g(x)=qx$, where $q=\frac{1\pm\sqrt{4n+1}}{2}$ (it is easy to check that these linear functions do satisfy the functional equation).  In terms of the original function $f$, the only solutions are $$f(x)=\frac{-1+\sqrt{4n+1}}{2}x$$ and $$f(x)=\frac{-1-\sqrt{4n+1}}{2}x.$$
