24. Find the number of functions $f$ from the set $S=\{0,1,2, \ldots, 2020\}$ to itself such that, for all $a, b, c \in S$, all three of the following conditions are satisfied:
(i) If $f(a)=a$, then $a=0$;
(ii) If $f(a)=f(b)$, then $a=b$; and
(iii) If $c \equiv a+b(\bmod 2021)$, then $f(c) \equiv f(a)+f(b)(\bmod 2021)$.
24. 1845
Solution. Note that, from (i), our function is completely determined by $f(1)$; i.e., $f(a) \equiv a f(1)$ (mod 2021). Then, from (i) and (ii), we need that $f(a) \neq 0$ if $a \neq 0$; otherwise, if $a \neq 0$ but $f(a)=0, f(b)=f(a+b)$ for any $b$. Thus, if $a \neq 0$, we need that $a f(1) \neq 0(\bmod 2021)$ for any $a \in S \backslash\{0\}$. If $\operatorname{gcd}(f(1), 2021)=d>1$, then note that $a=\frac{2021}{d} \in S \backslash\{0\}$, and from (i), $f(a) \equiv a f(1) \equiv 2021 \equiv 0(\bmod 2021)$. As we just established, this is not allowed. Hence, $\operatorname{gcd}(f(1), 2021)=1$.
Moreover, from (iii), we need that $f(a)=a f(1) \not \equiv a(\bmod 2021)$ if $a \neq 0$; in other words, $2021 \nmid a(f(1)-1)$ if $a \neq 0$. By similar reasoning to earlier, suppose that $\operatorname{gcd}(f(1)-1,2021)=$ $d>1$. Then $a=\frac{2021}{d} \in S \backslash\{0\}$, and 2021|a(f(1)-1); thus, $f(a)=a$. We thus need that $\operatorname{gcd}(f(1)-1,2021)=1$ as well.
We count the number of integers that satisfy both these conditions. We use complementary counting here; thus, we start by counting those that fail to satisfy at least one condition. Indeed, we count the number of values of $f(1)$ that are not coprime to 2021. Either they are divisible by 43 , or 47 , or both. Since only 0 is divisible by both, by the principle of inclusion and exclusion, there are $47+43-1=89$ possible values of $f(1)$ so that $f(1)$ is not coprime to 2021 . By the same reasoning, there are also 89 possible values of $f(1)$ such that $f(1)-1$ is not coprime to 2021 . Finally, we count the values of $f(1)$ for which neither $f(1)$ nor $f(1)-1$ is coprime to 2021 . This happens precisely when $43 \mid f(1)$ and $47 \mid f(1)-1$, or $47 \mid f(1)$ and $43 \mid f(1)-1$. By the Chinese remainder theorem, each of these possibilities gives one value of $f(1)$. Thus, by the principle of inclusion and exclusion, there are $2 \cdot 89-2=176$ such values of $f(1)$ that fail to satisfy at least one condition. This gives us $2021-176=1845$ possible values of $f(1)$, and thus possible functions $f$.