The only solution is f(a)=0 for a∈/S and f(x)=x for x∈S.
Label the equations as follows.
f(a+x+y)+f(f(a))+f(x)+f(y)=x+y(1)
f(axy)+f(a)+f(x)f(y)=xy(2)
Case 1. 0∈/S
Putting a=0 in (2), we obtain
f(x)f(y)=xy−2f(0)(3)
for all x,y∈S. In particular, by putting x=y, we get
f(x)2=x2−2f(0)(4)
for all x∈S. Then we have
(x2−2f(0))(y2−2f(0))=f(x)2f(y)2=(xy−2f(0))2,
which implies 2f(0)(x2+y2)=4f(0)xy. This means
2f(0)(x−y)2=0.
Since S has at least two elements, we can choose distinct x,y∈S to conclude
that f(0)=0. Therefore, by (4),
f(x)2=x2
for all x∈S. Since f(x)f(y)=xy by (3), we see that the choice of the sign for f(x) is independent of x. This means f(x)=x for all x∈S or f(x)=−x for all x∈S.
Now, (2) is reduced to
f(axy)+f(a)=0.(5)
For fixed nonzero a∈/S, if a1∈S, we may put y=a1 in (5) to get f(x)+f(a)=0. But this cannot be true as we can choose two different values for x (and hence f(x)). Therefore, we must have a1∈/S. From this, we see that x∈S implies x1∈S, since otherwise x=(x−1)−1∈/S.
Now, we can put y=x1 in (5) to obtain 2f(a)=0, i.e. f(a)=0 for any a∈/S. Equation (1) becomes
f(a+x+y)±(x+y)=x+y.
By putting x=y, we get
f(a+2x)±2x=2x.
If the negative sign is chosen, then we have f(a+2x)=4x. Since f(a+2x) can only be −(a+2x) or 0 (depending on whether a+2x∈S or not), we need a=−6x for any a∈/S and x∈S (note we cannot have 0=4x). This is impossible as we can find two distinct elements in S. Therefore, f(a)=0 for a∈/S and f(x)=x for x∈S.
Case 2. 0∈S
Putting y=0 in (2), we obtain
f(0)+f(a)+f(x)f(0)=0(6)
for all a∈/S, x∈S. In particular, by putting x=0, we get
f(a)=−f(0)−f(0)2.(7)
Then equation (6) becomes
f(x)f(0)=f(0)2.
If f(0)=0, we need f(x)=f(0) for all x∈S. Consider equation (1). It now becomes
f(a+x+y)+f(−f(0)−f(0)2)+2f(0)=x+y.
Note that the left-hand side can take at most two values (as f(a+x+y) can be −f(0)−f(0)2 or f(0)). However, as there are at least two elements in S, say 0 and z=0, the right-hand side can take at least three different values, namely, 0, z, 2z. This is a contradiction, and hence f(0)=0. By (7), we obtain f(a)=0 for a∈/S.
Next, we prove that a∈/S implies −a∈/S. Indeed, suppose −a∈S. We put y=−a in (1) to get
2f(x)+f(−a)=x−a.(8)
f(−a)=−a,(9)
and hence 2f(x)=x for any x∈S by (8). Now, by putting x=−a, we obtain 2f(−a)=−a. But then we have f(−a)=−a by (9). This forces a=0∈S, which is a contradiction. So the claim is true. From this, we see that x∈S implies −x∈S. Thus, we can put y=−x in (1). This gives
f(x)+f(−x)=0(10)
for any x∈S.
Similarly, we shall prove that z,w∈S implies z+w∈S. Suppose on the contrary that z+w∈/S. Note that −w∈S from above. So we can put a=z+w and x=0, y=−w in (1) to obtain
f(z)+f(−w)=−w.
Using (10), we get
f(z)−f(w)=−w.
By symmetry, we also have
f(w)−f(z)=−z.
Adding these, we obtain z+w=0∈S, which is a contradiction.
It is now clear that a+x+y∈/S for any a∈/S and x,y∈S. Otherwise, if a+x+y∈S, since −x−y=(−x)+(−y)∈S, we have
a=(a+x+y)+(−x−y)∈S.
It follows that (1) is reduced to f(x)+f(y)=x+y. Putting x=y, we get f(x)=x for any x∈S.
In any case, f(a)=0 for a∈/S and f(x)=x for x∈S is the only possible function. One can check that this is indeed a solution if and only if a+x+y,axy∈/S−{0} for any a∈/S and x,y∈S.