URAL 1132 Square Root(二次剩余)

1 篇文章 0 订阅

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值