Let A={a:(a,b)∈S} and B={b:(a,b)∈S}. The claim is equivalent to
2a∈A∑a=b∈B∑b
Assume that for some x,y,z∈{1,2,…,1008} both, x2+y2 and x2+z2, are multiples of 2017. By
(x2+y2)−(x2+z2)=y2−z2=(y+z)(y−z)≡0(mod2017),
0<y+z<2017 and the fact that 2017 is a prime number, it follows that y=z. Hence, A and B are disjoint, and there is a bijection f:A↦B such that for any a∈A and b∈B the pair (a,b) is in S if and only if b=f(a).
We show that the mapping g defined by g(a)=f(a)−a for a∈A is a bijection from A to A. Then (2) follows by
a∈A∑a=a∈A∑g(a)=a∈A∑f(a)−a∈A∑a=b∈B∑b−a∈A∑a
Let a∈A, and let h(a)=min{a+f(a),2017−(a+f(a))}. Then 0<2h(a)<2017 and, by the definition of g,0<2g(a)< 2017. Furthermore,
g(a)2+h(a)2≡(a−f(a))2+(a+f(a))2≡2(a2+f(a)2)≡0(mod2017).
If a+f(a)≤1008, then g(a)=f(a)−a<f(a)+a=h(a). If a+f(a)>1008, then g(a)=f(a)−a<(f(a)−a)+(2017−2f(a))= 2017−(a+f(a))=h(a). Consequently, g(a)∈A with f(g(a))=h(a).
It remains to show that g is injective. Assume that g(a1)=g(a2) for some a1,a2∈A, i.e.,
b1−a1=b2−a2,
where bi=f(ai) for i=1,2. Clearly, we also have h(a1)=h(a2) then. If h(a1)=a1+b1 and h(a2)=a2+b2, then subtracting (3) from a1+b1=a2+b2 gives a1=a2. Similarly, if h1(a1)=2017−(a1+b1) and h2=2017−(a2+b2), then we obtain a1=a2. Finally, if h(a1)=a1+b1 and h2=2017−(a2+b2), then 2(a1+b2)=2017, a contradiction.
Remark: The proof as given above obviously works for any prime congruent to 1 modulo 4 in the place of 2017. With a little more effort, one can show that the statement is true for any positive odd n (vacuously, if n has a prime factor congruent to 3 modulo 4).