Quadratic equation(二次剩余)2019牛客多校第九场

链接:https://ac.nowcoder.com/acm/contest/889/B
来源:牛客网

题目描述
Amy asks Mr. B problem B. Please help Mr. B to solve the following problem.

Let p = 1000000007.
Given two integers b and c, please find two integers x and y(0 \leq x \leq y < p)(0≤x≤y<p), such that
在这里插入图片描述
输入描述:
The first line contains an integer t, which is the number of test cases (1 <= t <= 10).

In the following t lines, each line contains two integers b and c (0 <= b, c < p).
输出描述:
For each test case, please output x, y in one line.
If there is a solution, because x <= y, the solution is unique.

If there is no solution, please output -1, -1
示例1
输入
复制
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
这个题是队友过的,用到了二次剩余定理,记录下来。

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll pow_mod(ll a, ll n, ll p) {
    ll res = 1;
    while (n) {
        if (n & 1) res = res * a % p;
        n >>= 1;
        a = a * a % p;
    }
    return res;
}
ll legendre(ll a, ll p) {
    return pow_mod(a, (p - 1) >> 1, p);
}
ll mod(ll a, ll p) {
    a %= p;
    if (a < 0) a += p;
    return a;
}
struct node {
    static ll p, omega;
    ll a, b;
    node(ll a, ll b): a(a % p), b(b % p) {}
};
node operator *(const node &p, const node &q) {
    int m = node::p;
    return node(p.a * q.a + p.b * q.b % m * node::omega,
                       p.a * q.b + q.a * p.b);
}
node pow_mod(node a, ll n) {
    node result(1, 0);
    while (n > 0) {
        if ((n & 1) == 1) {
            result = result * a;
        }
        a = a * a;
        n >>= 1;
    }
    return result;
}
ll node::p, node::omega;
ll modsqr(ll a, ll p) {
    if (p == 2) return 1;
    if (legendre(a, p) + 1 == p) return -1;
    if ((((p + 1) >> 1) & 1) == 0) return pow_mod(a, (p + 1) >> 2, p);
    ll a_0 = -1;
    while (true) {
        a_0 = rand() % p;
        if (legendre(mod(a_0 * a_0 - a, p), p) + 1 == p) break;
    }
    node::p = p;
    node::omega = mod(a_0 * a_0 - a, p);
    node ret = pow_mod(node(a_0, 1), (p + 1) >> 1);
    //assert(ret.b == 0);
    return ret.a;
}
const long long mood=1000000007;
int main ()
{
    ll b,c;
    ll s2=pow_mod(2,mood-2,mood);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld %lld",&b,&c);
        ll t=b*b-4*c;
        t=t%mood;
        ll x_y=modsqr(t,mood);
        if(x_y==-1)
        {
            printf("-1 -1\n");
        }
        else
        {
            if(x_y*2>mood)
                x_y=mood-x_y;
            ll x=(((x_y+b)%mood)*s2)%mood;
            ll y=((b-x)+mood)%mood;
            if(x>y)
                swap(x,y);
            if((x+y)%mood==b && (x*y)%mood==c)
                printf("%lld %lld\n",x,y);
            else
                printf("-1 -1\n");
             
        }
    }
    return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值