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
![](http://img.blog.csdn.net/20170119144556633?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVjVaU1E=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Code
给出两个正整数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; }
相关文章推荐
- URAL 1132 Square Root 暴力枚举,二次剩余.
- URAL - 1132 Square Root 二次剩余
- URAL1132 Square Root 二次剩余模版题
- [Ural 1132] Square Root (二次剩余方程)
- Ural 1132. Square Root 二次剩余
- timus 1132 Square Root (二次剩余)
- URAL 1123 Square Root(计算二次剩余)
- URAL 1132 二次剩余
- 【TOJ1132】Square Root,二次同余方程
- URAL 1132 Square Root <二次剩余 + 数论>
- ural 1132. Square Root
- URAL 1132 二次剩余
- [ural1132]Square Root(cipolla算法)
- URAL1132_Square Root
- 【TOJ1132】Square Root
- Timus1132(二次剩余方程求解)
- 【Timus Online Judge 1132】 Square Root
- [求解二次剩余 数论技巧 随机化] Ural 1132 Square Root
- Timus1132(二次剩余方程求解)
- find square root using "bisection method"