Quadratic deductions by simple modulus

Правка en8, от EJIC_B_KEDAX, 2024-07-29 01:34:33

Hello, Codeforces!

Today I'm going to talk about an unpopular technique in number theory.

Definition and elementary properties

Def. Let $$$p > 2$$$ be a prime number, then we will call quadratic residues all $$$1 \le x \le p - 1$$$ modulo $$$p$$$ such that the equation $$$a^2 \equiv x \pmod{p}$$$ has solutions and quadratic nonresidues otherwise. Note that $$$0$$$ is neither quadratic residue nor quadratic nonresidue.

Theorem: quadratic residues and quadratic nonresidues are equally divided.

Proof

Theorem: Denote by $$$R$$$ a quadratic residue and by $$$N$$$ a quadratic nonresidue, then:

$$$R \cdot R = R$$$
$$$N \cdot R = N$$$
$$$N \cdot N = R$$$

.

Proof

With these two theorems, we can already solve 103428K - Tiny Stars.

How to check if the number is residue or nonresidue?

There are several ways to check if a number is quadratic residue. In this blog, we will look at just one of them, you can read about Gauss's lemma and law of quadratic reciprocity.

Euler's criterion

$$$a$$$ is a quadratic residue modulo $$$p$$$ if and only if $$$a^{{\frac{p - 1}{2}}} \equiv 1 \pmod{p}$$$, and a quadratic nonresidue if and only if $$$a^{\frac{p - 1}{2}} \equiv -1 \pmod{p}$$$.

Proof

Consequence: $$$-1$$$ is quadratic residue if and only if $$$p = 4k + 1$$$, for some natural $$$k$$$, and quadratic nonresidue if and only if $$$p = 4k + 3$$$, for some natural $$$k$$$.

Clearly, the complexity of the number check is $$$O(\log_2p)$$$.

Code

Finding $$$i$$$ modulo $$$p$$$

Def. $$$i$$$ is such a number that $$$i^2 = -1$$$ $$$\implies$$$ $$$i$$$ modulo $$$p$$$ let us call such a number that $$$i ^ 2 \equiv -1 \pmod{p}$$$.

Algorithm: If $$$p = 4k + 3$$$ for some natural $$$k$$$, then there is no such $$$i$$$. If $$$p = 2$$$, then $$$i = 1$$$. Otherwise consider the quadratic nonresidue of $$$a$$$, by Euler's criterion we know that $$$a^{\frac{p - 1}{2}} \equiv -1 \pmod{p}$$$, then $$$a^{\frac{p - 1}{4}} \equiv i \pmod{p}$$$. All that remains is to find a quadratic nonresidue, for this we take a random $$$1 \le a \le p - 1$$$ and check it for $$$O(\log_2p)$$$, if it is a quadratic residue then take another random $$$a$$$. Since the number of residues and nonresidue are equal, we need $$$O(1)$$$ of such checks, so the expected running time of the algorithm is $$$O(\log_2p)$$$.

Code
Теги math, number theory

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en11 Английский EJIC_B_KEDAX 2024-07-30 15:37:55 0 (published)
ru16 Русский EJIC_B_KEDAX 2024-07-30 15:37:29 0 (опубликовано)
en10 Английский EJIC_B_KEDAX 2024-07-30 15:21:42 8
ru15 Русский EJIC_B_KEDAX 2024-07-30 15:14:38 11
en9 Английский EJIC_B_KEDAX 2024-07-30 15:14:09 74
en8 Английский EJIC_B_KEDAX 2024-07-29 01:34:33 29 Tiny change: 's, so the total complexity of the al' -> 's, so the expected running time of the al'
ru14 Русский EJIC_B_KEDAX 2024-07-29 01:34:09 26
en7 Английский EJIC_B_KEDAX 2024-07-29 01:32:56 96
en6 Английский EJIC_B_KEDAX 2024-07-29 01:30:19 1 Tiny change: 'y="Proof">.\nFirst, l' -> 'y="Proof">\nFirst, l'
en5 Английский EJIC_B_KEDAX 2024-07-29 01:29:57 117
en4 Английский EJIC_B_KEDAX 2024-07-29 01:26:48 424
ru13 Русский EJIC_B_KEDAX 2024-07-29 01:19:13 55
en3 Английский EJIC_B_KEDAX 2024-07-29 01:18:47 97 Tiny change: 's's lemma](https://en' -> 's's lemma] https://en'
en2 Английский EJIC_B_KEDAX 2024-07-29 01:09:44 23
en1 Английский EJIC_B_KEDAX 2024-07-29 00:54:10 6734 Initial revision for English translation (saved to drafts)
ru12 Русский EJIC_B_KEDAX 2024-07-29 00:46:05 1 Мелкая правка: 'Привет Codeforce' -> 'Привет, Codeforce'
ru11 Русский EJIC_B_KEDAX 2024-07-22 02:26:00 5
ru10 Русский EJIC_B_KEDAX 2024-07-22 02:23:16 1674 Мелкая правка: 'ществует. Иначе ра' -> 'ществует. Если $p = 2$, то $i = 1$. Иначе ра'
ru9 Русский EJIC_B_KEDAX 2024-07-22 01:53:33 2378 Мелкая правка: 'oiler>\n\n' -> 'oiler>\n\nНахождение $i$ по модулю $p$\n------------------'
ru8 Русский EJIC_B_KEDAX 2024-07-22 00:46:55 17 Мелкая правка: ' while (x) {\n ' -> ' while (x != 0) {\n '
ru7 Русский EJIC_B_KEDAX 2024-07-22 00:20:46 2467 Мелкая правка: '428K].\n\nКак пров' -> '428K].\n\n==================\nКак пров'
ru6 Русский EJIC_B_KEDAX 2024-07-21 22:58:36 18
ru5 Русский EJIC_B_KEDAX 2024-07-21 22:46:10 73 Мелкая правка: '**Опр.** П' -> '[problem:103428K]**Опр.** П'
ru4 Русский EJIC_B_KEDAX 2024-07-21 22:40:56 1656 Мелкая правка: 'd{p}$.\n\n$$H \c' -> 'd{p}$.\n\n\n$$H \c'
ru3 Русский EJIC_B_KEDAX 2024-07-21 21:39:52 325 Мелкая правка: 'oiler>\n\n' -> 'oiler>\n\n\n'
ru2 Русский EJIC_B_KEDAX 2024-07-21 21:30:49 482 Мелкая правка: 'n**Теорема**: Квадратич' -> 'n**Теорема:** Квадратич'
ru1 Русский EJIC_B_KEDAX 2024-07-21 21:12:13 344 Первая редакция (сохранено в черновиках)