Example 3 Given positive integers $m>1, n$, $m$ is odd and $m \mid n$, let $X$ be a non-empty subset of the set $\{1,2, \cdots, n\}$, and $S(X)$ be the sum of all elements in the set $X$. Find the number of all subsets $X$ such that $m \mid S(X)$.
Let $f(x)=(1+x)\left(1+x^{2}\right) \cdots\left(1+x^{n}\right)$, and let $A$ denote the sum of the coefficients of the terms $x^{m k}\left(k \in \mathbf{Z}_{+}\right)$ in the expansion of $f(x)$. Then $A-1$ is the desired result.
Let $\varepsilon (\varepsilon \neq 1)$ be an $m$-th root of unity. Then
$$
1+\varepsilon+\varepsilon^{2}+\cdots+\varepsilon^{m-1}=0.
$$

Let $f(x)=1+a_{1} x+\cdots+a_{N} x^{N}$.
Then $f(1)=1+a_{1}+\cdots+a_{N}$,
......
$$
f\left(\varepsilon^{m-1}\right)=1+a_{1} \varepsilon^{m-1}+\cdots+a_{N} \varepsilon^{N(m-1)}.
$$

Adding the above $m$ equations, we get
$$
A=\frac{f(1)+f(\varepsilon)+\cdots+f\left(\varepsilon^{m-1}\right)}{m}.
$$

Given $f(1)=2^{n}$, for $1 \leqslant k \leqslant m-1$, let $n=r m\left(r \in \mathbf{Z}_{+}\right)$,
then $f\left(\varepsilon^{k}\right)=\prod_{i=1}^{n}\left(1+\varepsilon^{i k}\right)=\left[\prod_{j=0}^{m-1}\left(1+\varepsilon^{j}\right)\right]^{r}$.
From $x^{m}-1=(x-1) \prod_{j=1}^{m-1}\left(x-\varepsilon^{j}\right)$, taking $x=-1$ we get
$$
\begin{array}{l}
\prod_{j=1}^{m-1}\left(1+\varepsilon^{j}\right)=1 \Rightarrow f\left(\varepsilon^{k}\right)=2^{r} \\
\Rightarrow A=\frac{2^{n}+(m-1) 2^{r}}{m}.
\end{array}
$$

Thus, the number of subsets $X$ such that $m \mid S(X)$ is
$$
A-1=\frac{2^{n}+(m-1) 2^{r}}{m}-1.
$$