URAL 1132. Square Root(二次剩余)

URAL 1132. Square Root

传送门

description

The number x is called a square root of a modulo n (root(a,n)) if x*x = a (mod n). Write the program to find the square root of number a by given modulo n.

Input

One number K in the first line is an amount of tests (K ≤ 100000). Each next line represents separate test, which contains integers a and n (1 ≤ a, n ≤ 32767, n is prime, a and n are relatively prime).

Output

For each input test the program must evaluate all possible values root(a,n) in the range from 1 to n − 1 and output them in increasing order in one separate line using spaces. If there is no square root for current test, the program must print in separate line: ‘No root’.
Sample

input

5
4 17
3 7
2 7
14 31
10007 20011

output

2 15
No root
3 4
13 18
5382 14629

首先这道题要我们找x满足x2=a(mod n)
这个就是典型的二次剩余,题目已经说过n为质数,所以n一定和a互质,所以我们只需要n为质数的情况就行了,首先考虑n为2的特殊情况,由于n和a范围都是1到32767,故当n为2的时候答案为0或者1,因为答案规定在0~n-1范围内,所以当a为偶数的时候就是0,奇数就是1.
其次考虑一般情况,n为奇素数:

勒让德符号

( n p ) = { 1 , n 在 模 p 意 义 下 是 二 次 剩 余 − 1 , n 在 模 p 意 义 下 是 非 二 次 剩 余 0 , n ≡ 0 ( m o d   p ) (\frac{n}{p})= \begin{cases} 1,n在模p意义下是二次剩余\\-1,n在模p意义下是非二次剩余\\0,n\equiv0(mod~p) \end{cases} (pn)=1,np1,np0,n0(mod p)
对于勒让德符号有 ( n p ) = n p − 1 2 ( m o d   p ) (\frac{n}{p})=n^\frac{p-1}{2}(mod~p) (pn)=n

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值