Euler's Criterion
Your friend gives you an equation A≡X2(modM) and asks you to find an integer solution for X .
However, you know your friend's mischievous nature and suspect that there is no solution to such an equation. Thus, you first want to find out whether there is a solution to it.
You may find this link helpful: http://en.wikipedia.org/wiki/Euler%27s_criterion
Input Format
The first line contains the number of cases, T . T lines follow, each containing two integers A and M separated by a single space.
Output Format
Output T lines, each containing one word:
YES, if a solution exists and NO otherwise.
Constraints
Sample Input
2
5 7
4 7
Sample Output
NO
YES
Explanation
In the second test case, we can take X=2 , as 4≡22(mod7) . Or we can take X=5 , as 52=25≡4(mod7) .
However there is no integer which gives 5 modulo 7 when squared.
-------------------------------------------editorial---------------------------
Editorial by shashank21j
The approach can be understood from the Wikipedia article on Euler's criterion.
You also need to take care of two special cases where Euler's criterion does not apply:
You also need to take care of two special cases where Euler's criterion does not apply:
- `
A=0 ` (`A ` and `M ` are not coprime) - `
M=2 ` (`M ` is not an odd prime)
Also, note that many languages don't have a native
modpow function (which can calculate powers modulo `
-------------------------------------------wiki--------------------------------------
Euler's criterion
From Wikipedia, the free encyclopedia
In number theory Euler's criterion is a formula for determining whether an integer is a quadratic residue modulo a prime. Precisely,
Euler's criterion can be concisely reformulated using the Legendre symbol:[2]
Contents
[hide]Proof[edit]
The proof uses the fact that the residue classes modulo a prime number are a field. See the article prime field for more details. The fact that there are (p − 1)/2 quadratic residues and the same number of nonresidues (mod p) is proved in the article quadratic residue.
Fermat's little theorem says that
(Assume throughout this solution that a is not 0 mod p). This can be written as
Since the integers mod p form a field, one or the other of these factors must be congruent to zero.
Now if a is a quadratic residue, a ≡ x2,
So every quadratic residue (mod p) makes the first factor zero.
Lagrange's theorem says that there can be no more than (p − 1)/2 values of a that make the first factor zero. But it is known that there are (p − 1)/2 distinct quadratic residues (mod p) (besides 0). Therefore they are precisely the residue classes that make the first factor zero. The other (p − 1)/2 residue classes, the nonresidues, must be the ones making the second factor zero. This is Euler's criterion.
Examples[edit]
Example 1: Finding primes for which a is a residue
Let a = 17. For which primes p is 17 a quadratic residue?
We can test prime p's manually given the formula above.
In one case, testing p = 3, we have 17(3 − 1)/2 = 171 ≡ 2 ≡ −1 (mod 3), therefore 17 is not a quadratic residue modulo 3.
In another case, testing p = 13, we have 17(13 − 1)/2 = 176 ≡ 1 (mod 13), therefore 17 is a quadratic residue modulo 13. As confirmation, note that 17 ≡ 4 (mod 13), and 22 = 4.
We can do these calculations faster by using various modular arithmetic and Legendre symbol properties.
If we keep calculating the values, we find:
- (17/p) = +1 for p = {13, 19, ...} (17 is a quadratic residue modulo these values)
- (17/p) = −1 for p = {3, 5, 7, 11, 23, ...} (17 is not a quadratic residue modulo these values).
Example 2: Finding residues given a prime modulus p
Which numbers are squares modulo 17 (quadratic residues modulo 17)?
We can manually calculate it as:
- 12 = 1
- 22 = 4
- 32 = 9
- 42 = 16
- 52 = 25 ≡ 8 (mod 17)
- 62 = 36 ≡ 2 (mod 17)
- 72 = 49 ≡ 15 (mod 17)
- 82 = 64 ≡ 13 (mod 17).
So the set of the quadratic residues modulo 17 is {1,2,4,8,9,13,15,16}. Note that we did not need to calculate squares for the values 9 through 16, as they are all negatives of the previously squared values (e.g. 9 ≡ −8 (mod 17), so 92 ≡ (−8)2 = 64 ≡ 13 (mod 17)).
We can find quadratic residues or verify them using the above formula. To test if 2 is a quadratic residue modulo 17, we calculate 2(17 − 1)/2 = 28 ≡ 1 (mod 17), so it is a quadratic residue. To test if 3 is a quadratic residue modulo 17, we calculate 3(17 − 1)/2 = 38 ≡ 16 ≡ −1 (mod 17), so it is not a quadratic residue.
Euler's criterion is related to the Law of quadratic reciprocity and is used in a definition of Euler–Jacobi pseudoprimes.
------------------------------------------------------------------ii---------------------------------------------------------------





No comments:
Post a Comment