您的位置:首页 > 其它

URAL 1132 Square Root(二次剩余)

2017-01-19 14:47 429 查看
Description

给出两个正整数a和n,求x满足x*x=a(mod n)

Input

第一行一整数T为用例组数,每组用例占一行包括两个正整数a和n

(T<=100000,1<=a,n<=32767)

Output

对于每组用例,如果存在x满足x*x=a(mod n)则从小到大输出所有满足条件的x,否则输出“No root”

Sample Input

5

4 17

3 7

2 7

14 31

10007 20011

Sample Output

2 15

No root

3 4

13 18

5382 14629

Solution



Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
struct T
{
ll x,y;
T(){};
T(ll _x,ll _y)
{
x=_x,y=_y;
}
};
ll w;
T mul(T a,T b,ll p)
{
T ans;
ans.x=(a.x*b.x%p+a.y*b.y%p*w%p)%p;
ans.y=(a.x*b.y%p+a.y*b.x%p)%p;
return ans;
}
T T_mod_pow(T a,ll b,ll p)
{
T ans=T(1,0);
while(b)
{
if(b&1)ans=mul(ans,a,p);
a=mul(a,a,p);
b>>=1;
}
return ans;
}
ll mod_pow(ll a,ll b,ll p)
{
ll ans=1;;
while(b)
{
if(b&1)ans=ans*a%p;
a=a*a%p;
b>>=1;
}
return ans;
}
ll Solve(ll a,ll p)//求解x^2=a(mod p)
{
a%=p;
if(p==2)return a;
if(mod_pow(a,(p-1)/2,p)==p-1)return -1;
int b,t;
while(1)
{
b=rand()%p;
t=b*b-a;
w=(t%p+p)%p;
if(mod_pow(w,(p-1)/2,p)==p-1)break;
}
T ans=T(b,1);
ans=T_mod_pow(ans,(p+1)/2,p);
return ans.x;
}
int main()
{
int T,n,p;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&p);
int x=Solve(n,p);
if(x==-1)printf("No root\n");
else
{
int y=p-x;
if(x>y)swap(x,y);
if(x==y)printf("%d\n",x);
else printf("%d %d\n",x,y);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: