样例输入:
10
4 4
5 6
10 10
10 25
20000 100000000
0 5
3 6
220 284
0 1
1000000000 1000000000
输出:
2 2
2 3
-1 -1
5 5
10000 10000
474848249 525151758
352077071 647922939
448762649 551237578
-1 -1
366417496 633582504
解方程组,在这里我是先求y,再求x;
怎么套二次剩余模板呢;
用判断一元二次方程的:b^2-4ac来套模板,看得到的答案是不是大于等于0的,如果是就有解,否则没有
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define random(a,b) (rand()%(b-a+1)+a)
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
const int p=1e9+7;
struct hh{
ll x,y;
hh(){};
hh(ll _x,ll _y){
x=_x;y=_y;
}
};
ll w;
hh mul(hh a,hh b,ll p){
hh 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;
}
hh quick1(hh a,ll b,ll p){
hh ans=hh(1,0);
while(b){
if(b&1) ans=mul(ans,a,p);
a=mul(a,a,p);
b>>=1;
}
return ans;
}
ll quick2(ll a,ll b,ll p){
ll ans=1;
while(b){
if(b&1) ans=(ans*a)%p;
b>>=1;
a=(a*a)%p;
}
return ans;
}
ll solve(ll a,ll p){//求解 x^2=a(mod p) 的x的值
a%=p;//注意这句话
if(a==0) return 0;//注意这句话
if(p==2) return a;
if(quick2(a,(p-1)/2,p)==p-1) return -1;
ll b,t;
while(1){
b=rand()%p;
t=b*b-a;
w=(t%p+p)%p;
if(quick2(w,(p-1)/2,p)==p-1) break;
}
hh ans=hh(b,1);
ans=quick1(ans,(p+1)/2,p);
return ans.x;
}
ll inv(ll base,ll num){
ll ans=1;
while(num){
if(num&1)
ans=(ans*base)%p;
num>>=1;
base=(base*base)%p;
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int T;
scanf("%d",&T);
ll b,c;
while(T--){
scanf("%lld%lld",&b,&c);
ll t=b*b-4ll*c;
t=(t+p)%p;
t= solve(t,p);
if(t<0){
printf("-1 -1\n");
}else{
ll x=((b+t)%p*inv(2,p-2))%p;
ll y=(b-x+p)%p;
if(x<=y)
printf("%lld %lld\n",x,y);
else
printf("%lld %lld\n",y,x);
}
}
return 0;
}
模板
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define random(a,b) (rand()%(b-a+1)+a)
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
const int p=1e9+7;
struct hh{
ll x,y;
hh(){};
hh(ll _x,ll _y){
x=_x;y=_y;
}
};
ll w;
hh mul(hh a,hh b,ll p){
hh 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;
}
hh quick1(hh a,ll b,ll p){
hh ans=hh(1,0);
while(b){
if(b&1) ans=mul(ans,a,p);
a=mul(a,a,p);
b>>=1;
}
return ans;
}
ll quick2(ll a,ll b,ll p){
ll ans=1;
while(b){
if(b&1) ans=(ans*a)%p;
b>>=1;
a=(a*a)%p;
}
return ans;
}
ll solve(ll a,ll p){//求解 x^2=a(mod p) 的x的值
a%=p;//注意这句话
if(a==0) return 0;//注意这句话
if(p==2) return a;
if(quick2(a,(p-1)/2,p)==p-1) return -1;
ll b,t;
while(1){
b=rand()%p;
t=b*b-a;
w=(t%p+p)%p;
if(quick2(w,(p-1)/2,p)==p-1) break;
}
hh ans=hh(b,1);
ans=quick1(ans,(p+1)/2,p);
return ans.x;
}
ll inv(ll base,ll num){
ll ans=1;
while(num){
if(num&1)
ans=(ans*base)%p;
num>>=1;
base=(base*base)%p;
}
return ans;
}
int main()
{
ll t= solve(t,p);
return 0;
}