Number of elements of $\mathbb{Z}_p$ that satisfy a certain property Let $S(n,p)=\{a\in \mathbb{Z}_p : a^n=1$ (mod $p$)$\}$ where $p\geq3$ is a prime number and $1\leq n\leq p$. I am interested in finding a general formula for cardinality of $S(n,p)$. For example, I know that $|S(1,p)|=1$ and $|S(p-1,p)|=p-1$ (by Fermat's little theorem).

Already this has been answered. But I'd like to make two points: as $0$ is ruled out we are looking at a subset of the group $\mathbf{Z}_p^*$. And $x\mapsto x^n$ is a homomorphism of this group, and so the solution set $S(n,p)$ you are looking for is the kernel of that homomorphism.
