2019牛客暑期多校训练营(第九场)Quadratic equation

题意:

思路:

已知

(x+y)^{2}\;mod\; p=b*b\; mod\; p

4xy\; mod\;p=4*c\; mod\; p

求:

(x-y)^{2}/;mod/;p=?

x+y=? x-y=? 即可求解

x^{2} \; mod\;p=a 可用二次剩余求解

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=b;i>=a;i--)
using namespace std;
#define ll long long
const int N=3e5+5;
const int mod = 1e9+7;
ll B,c;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
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;
}
bool check(ll b,ll x)
{
	if((b+x)%2==1) 
	{
		//printf("-1 -1\n");
		return 0;
	}
	ll X=(b+x)/2,Y=(b-x)/2;
	if(0<=X&&X<mod&&Y<mod&&Y>=0) 
	{
		//printf("%lld %lld\n",X,Y);
		return 1;
	}
	//printf("-1 -1\n");
	return 0;
}
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);
		if(check(B,x)) printf("%d %d\n",(B-x)/2,(B+x)/2);
		else if(check(B+p,x)) printf("%d %d\n",(B+p-x)/2,1ll*(B+p+x)/2);
		else printf("-1 -1\n");
	}
    else 
	{
		//printf("%d %d\n",p-x,x);
		if(check(B,x)) printf("%d %d\n",(B-x)/2,(B+x)/2);
		else if(check(B,p-x)) printf("%d %d\n",(B-(p-x))/2,(B+p-x)/2);
		else if(check(B+p,x)) printf("%d %d\n",(B+p-x)/2,(B+p+x)/2);
		else if(check(B+p,p-x)) printf("%d %d\n",(B+p-(p-x))/2,(B+p+p-x)/2);
		else printf("-1 -1\n");	
	}
}        
void init(ll a,ll n)
{
//	printf("%lld\n",a);
	a%=n;
	if(a==0) 
	{
		if(B%2==0) printf("%d %d\n",B/2,B/2);
		else printf("%d %d\n",(B+mod)/2,(B+mod)/2);
		return;
	}
    if(!Legendre(a,n)) 
    {
        puts("-1 -1");
        return ;
    }
    modsqr(a,n);
}
int main()
{
    int T;
    scanf("%d",&T);
    int a,n;
    while(T--)
    {
    	scanf("%lld%lld",&B,&c);
    	init((B*B%mod-4ll*c%mod+mod)%mod,mod);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值