题意:
思路:
已知
求:
x+y=? x-y=? 即可求解
可用二次剩余求解
#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;
}