Timus 1132 Square Root (quadratic residual solution 2)

I don't understand, backboard

#include<cstdio>

using namespace std;

int Pow(int a,int b,int p)
{
    int res=1;
    for(;b;a=1LL*a*a%p,b>>=1)
        if(b&1) res=1LL*a*res%p;
    return res;
}

bool Legendre(int a,int p)
{
    return Pow(a,p-1>>1,p)==1;
}

void modsqr(int a,int p)
{
    int x;
    int i,k,b;
    if(p==2) x=a%p;
    else if(p%4==3) x=Pow(a,p+1>>2,p);
    else
    {
        for(b=1;Legendre(b,p);++b);
        i=p-1>>1;
        k=0;
        do
        {
            i>>=1;
            k>>=1;
            if(!((1LL*Pow(a,i,p)*Pow(b,k,p)+1)%p)) k+=p-1>>1;
        }while(!(i&1));
        x=1LL*Pow(a,i+1>>1,p)*Pow(b,k>>1,p)%p;
    }
    if(p-x<x) x=p-x;
    if(x==p-x) printf("%d\n",x);
    else printf("%d %d\n",x,p-x);
}        

intmain ()
{
    freopen("data.txt","r",stdin);
    freopen("aa.txt","w",stdout);
    int T;
    scanf("%d",&T);
    int a,n;
    while(T--)
    {
        scanf("%d%d",&a,&n);
        a%=n;
        if(!Legendre(a,n)) 
        {
            puts("No root");
            continue;
        }
        modsqr(a,n);
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325050062&siteId=291194637