题目链接:https://ac.nowcoder.com/acm/contest/889/B
题意:Let p = 1000000007. Given two integers b and c, please find two integers x and y(0≤x≤y<p) such that
(x+y) % p=b (x*y) %p=c b,c都小于1e9+7
思路:首先我们可以把给的条件化成 -x²+bx-c与0关于p同余,然后按下面的步骤转换:
如果p % 4 =3,x² % p = a ,那么x = ±pow(a, (p+1)/4, p),我们就可以求上述方程的解了。
最后是AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int p=1e9+7;
LL b,c,x,y;
int t;
LL quickpow(LL a,LL m)
{
LL res=1;
LL b=a;
while(m)
{
if(m&1)
res=res*b%p;
b=b*b%p;
m>>=1;
}
return res;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&b,&c);
LL d=b*quickpow(2,p-2)%p;
LL dd=(d*d-c+p)%p;
dd=quickpow(dd,(p+1)/4);
x=(d+dd)%p;
y=(b+p-x)%p;
if (x>y)
swap(x,y);
if ((x+y)%p!=b||(x*y)%p!=c)
printf("-1 -1\n");
else
printf("%lld %lld\n",x,y);
}
return 0;
}
数论真是太好玩了