2019牛客暑期多校训练营(第九场)B Quadratic equation(二次剩余定理)

题目链接: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;
}

数论真是太好玩了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值