show this $\prod_{i=1}^{\frac{p-1}{2}}a_{i}\equiv\prod_{i=1}^{\frac{p-1}{2}}b_{i}\pmod {p^3}$

let $p=8k+1$ prime number, show that there exist two set $A=\{a_{1},a_{2},\cdots,a_{\frac{p-1}{2}}\},B=\{b_{1},b_{2},\cdots,b_{\frac{p-1}{2}}\}$ such that $A\bigcup B=\{1,2,3,\cdots,p-1\},A\bigcap B=\varnothing $,and $$\prod_{i=1}^{\frac{p-1}{2}}a_{i}\equiv\prod_{i=1}^{\frac{p-1}{2}}b_{i}\pmod {p^3}$$

This problem should be treated with the knowledge of quadratic residue.


This problem appeared in Kvant (M2300). I guess it is more classical than that. I will quote the construction given in the solution.

Let $p-1=4k$. Let us note that the number of odd quadratic residues is equal to the number of even quadratic residues; same goes for non residues. This is because $x$ and $p-x$ have the same Legendre symbol; $-1$ is a square for $p\equiv 1 \pmod{4}$. There are $2k$ nonzero quadratic residues and $2k$ quadratic residues. We denote by $a_1, \ldots, a_k$ the odd quadratic residues, $b_1=p-a_1,\ldots, b_k=p-a_k$ the even quadratic residues, $c_1,\ldots, c_k$ the odd quadratic nonresidues, and finally $d_1=p-c_1,\ldots, d_k=p-c_k$ the even quadratic nonresidues. The claim is we can take $\mathcal{A}=\{a_1,\ldots, a_k, d_1,\ldots, d_k\}$ and $\mathcal{B}=\{b_1,\ldots, b_k, c_1,\ldots, c_k\}$. Note that this is consistent with the above numerical constructions given in the comments.

Let $A=a_1\ldots a_k$ and similarly we define $B,C,D$. Let us obtain first a relation between $A$ and $B$. Note that $2^{2k}AB$ is a product of all even numbers which are quadratic residues, but this time between $1$ and $2p-1$. These can only be $b_1, b_2,\ldots, b_k, p+a_1, \ldots, p+a_k$ in some order . Thus $$2^{2k} AB=\prod_{i=1}^k (p-a_i)(p+a_i)=\prod_{i=1}^k (p^2-a_i^2)\equiv A^2\left(1-p^2\sum_{i=1}^k \frac{1}{a_i^2}\right) \pmod{p^3}.$$ Now note that $2\displaystyle \sum_{i=1}^k \frac{1}{a_i^2}\equiv \sum_{i=1}^{k} \frac{1}{a_i^2}+\sum_{i=1}^{k} \frac{1}{b_i^2}\pmod{p}$. If we invert a quadratic residues it is still a quadratic residue thus $\displaystyle \sum_{i=1}^{k} \frac{1}{a_i^2}+\sum_{i=1}^{k} \frac{1}{b_i^2}\equiv \sum_{i=1}^k a_i^2+\sum_{i=1}^k b_i^2\pmod{p}$. If we take a primitive root $g$ mod $p$ the quadratic residues are $g_i^{2j}$ for $j=0,\ldots, 2k-1$. Thus $$\sum_{i=1}^k a_i^2+\sum_{i=1}^k b_i^2\equiv \sum_{j=0}^{2k-1} (g^4)^{j}=\cfrac{g^{8k}-1}{g^4-1}\equiv 0\pmod{p} $$ since $p>5$. Putting it all together $2^{2k} AB\equiv A^2 \pmod{p^3}$ and thus $B\equiv 2^{-2k} A\pmod{p^3}$. A similar argument shows that $C\equiv 2^{2k} D \pmod{p^3}$ and we are done.