Quadratische Reste modulo einer Primzahl

Rust Medium math

Aufgabe «Quadratische Reste modulo einer Primzahl»

Gegeben ist eine Primzahl p (p > 2) und eine ganze Zahl a (0 ≤ a < p). Es muss bestimmt werden, ob a ein quadratischer Rest modulo p ist, das heißt, ob es eine ganze Zahl x gibt, so dass x^2 ≡ a (mod p). Wenn a ein quadratischer Rest ist, geben Sie ein beliebiges solches x (0 ≤ x < p) aus. Wenn nicht, geben Sie -1 aus. Verwenden Sie das Legendre-Symbol und den Tonelli-Shanks-Algorithmus oder eine Brute-Force-Methode unter Berücksichtigung der Einschränkungen.

Eingabeformat

Die erste Zeile enthält zwei ganze Zahlen: p (Primzahl, 3 ≤ p ≤ 10^9) und a (0 ≤ a < p).

Ausgabeformat

Geben Sie eine ganze Zahl aus: x (0 ≤ x < p) mit x^2 ≡ a (mod p) oder -1, falls keine Lösung existiert.

Beispiele

Beispiel 1

EINGABE
7 2
AUSGABE
4

Beispiel 2

EINGABE
7 apple banana apple cherry banana apple date
AUSGABE
-1

Beispiel 3

EINGABE
7 3
AUSGABE
-1

Beispiel 4

EINGABE
4 aa a aaa a
AUSGABE
-1

Beispiel 5

EINGABE
13 4
AUSGABE
11
main.rs