Suppose that function f:N→N satisfies the problem conditions.
Lemma. For any prime p and any x,y∈N, we have x≡y(modp) if and only if f(x)≡f(y)(modp). Moreover, p∣f(x) if and only if p∣x.
Proof. Consider an arbitrary prime p. Since f is surjective, there exists some x∈N such that p∣f(x). Let
d=min{x∈N:p∣f(x)}
By induction on k, we obtain that p∣f(kd) for all k∈N. The base is true since p∣f(d). Moreover, if p∣f(kd) and p∣f(d) then, by the problem condition, p∣f(kd+d)=f((k+1)d) as required.
Suppose that there exists an x∈N such that d∤x but p∣f(x). Let
y=min{x∈N:d∤x,p∣f(x)}.
By the choice of d, we have y>d, and y−d is a positive integer not divisible by d. Then p∤f(y−d), while p∣f(d) and p∣f(d+(y−d))=f(y). This contradicts the problem condition. Hence, there is no such x, and
p∣f(x)⟺d∣x.(1)
Take arbitrary x,y∈N such that x≡y(modd). We have p∣f(x+(2xd−x))=f(2xd); moreover, since d∣2xd+(y−x)=y+(2xd−x), we get p∣f(y+(2xd−x)). Then by the problem condition p∣f(x)+f(2xd−x), p∣f(y)+f(2xd−x), and hence f(x)≡−f(2xd−x)≡f(y)(modp).
On the other hand, assume that f(x)≡f(y)(modp). Again we have p∣f(x)+f(2xd−x) which by our assumption implies that p∣f(x)+f(2xd−x)+(f(y)−f(x))=f(y)+f(2xd−x). Hence by the problem condition p∣f(y+(2xd−x)). Using (1) we get 0≡y+(2xd−x)≡y−x(modd).
Thus, we have proved that
x≡y (modd)⟺f(x)≡f(y) (modp).(2)
We are left to show that p=d: in this case (1) and (2) provide the desired statements.
The numbers 1,2,…,d have distinct residues modulo d. By (2), numbers f(1),f(2),…,f(d) have distinct residues modulo p; hence there are at least d distinct residues, and p≥d. On the other hand, by the surjectivity of f, there exist x1,…,xp∈N such that f(xi)=i for any i=1,2,…,p. By (2), all these xi's have distinct residues modulo d. For the same reasons, d≥p. Hence, d=p.
Now we prove that f(n)=n by induction on n. If n=1 then, by the Lemma, p∤f(1) for any prime p, so f(1)=1, and the base is established. Suppose that n>1 and denote k=f(n). Note that there exists a prime q∣n, so by the Lemma q∣k and k>1.
If k>n then k−n+1>1, and there exists a prime p∣k−n+1; we have k≡n−1(modp). By the induction hypothesis we have f(n−1)=n−1≡k=f(n)(modp). Now, by the Lemma we obtain n−1≡n(modp) which cannot be true.
Analogously, if k<n, then f(k−1)=k−1 by induction hypothesis. Moreover, n−k+1>1, so there exists a prime p∣n−k+1 and n≡k−1(modp). By the Lemma again, k=f(n)≡f(k−1)=k−1(modp), which is also false. The only remaining case is k=n, so f(n)=n.
Finally, the function f(n)=n obviously satisfies the condition.