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

更多多校信息←请点击这里

B.Quadratic equation

传送门
题目描述
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 ≤ x ≤ y &lt; p ) (0 \leq x \leq y &lt; p) (0xy<p) such that
( x + y ) &VeryThinSpace; m o d &VeryThinSpace; p = b (x + y) \bmod p= b (x+y)modp=b
( x × y ) &VeryThinSpace; m o d &VeryThinSpace; p = c (x \times y) \bmod p = c (x×y)modp=c
输入描述:
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


题目大意:
给你b,c,解
( x + y ) &VeryThinSpace; m o d &VeryThinSpace; p = b (x + y) \bmod p= b (x+y)modp=b
( x × y ) &VeryThinSpace; m o d &VeryThinSpace; p = c (x \times y) \bmod p = c (x×y)modp=c
输出x,y


题目思路:

首先知道上面这个式子等于:
( x + y ) ≡ b (x + y) ≡ b (x+y)b (mod p)
( x × y ) ≡ c (x \times y) ≡ c (x×y)c (mod p)

这些公式可以进行一下运算:

  • 反身性:a≡a (mod m)
  • 对称性:若a≡b(mod m),则b≡a(mod m)
  • 传递性:若a≡b(mod m),b≡c(mod m),则a≡c(mod m)
  • 同余式相加:若a≡b(mod m),b≡c(mod m),则a ± c≡b ± d(mod m)
  • 同余式相乘:若a≡b(mod m),b≡c(mod m),则ac≡bd(mod m)
  • 线性运算:如果a≡b(mod m),c≡d(mod m),那么a ± c≡b ± d(mod m),且a *c≡b*d(mod m)
  • 除法:若ac≡bc(mod m) c≠0则 a≡b(mod m/gcd(c,m)) 其中gcd(c,m)表示c,m的最大公约数。特殊地,gcd(c,m)=1 则a≡b(mod m)
  • 幂运算:如果a≡b(mod m),那么a^n≡b^n(mod m)
  • 若a≡b(mod m),n|m,则a≡b(mod n)
  • 若a≡b (mod mi) (i=1,2…n) 则 a≡b (mod [m1,m2,…mn]) 其中[m1,m2,…mn]表示m1,m2,…mn的最小公倍数

所以这道题可以化为:
可化成 ( 2 x − b ) 2 ≡ b 2 − 4 c ( m o d p ) (2x−b)2≡b2−4c(mod p) (2xb)2b24c(modp),然后核心问题是解 x 2 ≡ a ( m o d p ) x2≡a(mod p) x2a(modp)这个方程,就是典型的二次剩余题目了。


直接套模板
ac_code:

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll p=1e9+7;
const ll inv2=500000004;  //1/2取mod
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的值,无解返回-1
    a = (a%p + p)%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 b, c;

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%lld%lld",&b,&c);
        ll two=solve(b*b-4*c,p); //求解 x^2=a(mod p) 的x的值,无解返回-1
        if(two==-1)  printf("-1 -1\n");
        else{
            ll x=(b+two)*inv2%p;  //inv=1/2取mod
            ll y=(b-x+p)%p;  
            if(x>y) swap(x,y);
            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、付费专栏及课程。

余额充值