\((x+y)\equiv b\pmod p\)

\((x\times y)\equiv c\pmod p\)

由第一个式子可知:\(x+y=b~or~x+y=b+p\)

先任选一个代入到第二个式子里得

\[(x\times(b-x))\equiv c\pmod p
\Rightarrow (2*x-b)^2\equiv (b^2-4c)\pmod p
\]

解二次剩余方程 \(q^2\equiv a\pmod p\)

因为这个方程去查了很多资料

1. 欧拉准则

对于\(x^2\equiv a\pmod p\)

\[a^{p-1\over 2} =
\begin{cases}
1\pmod p & {如果存在一个x使得a \equiv x^2 \pmod p}\\
-1\pmod p & {如果不存在x使得上式成立}
\end{cases}
\]

证明:

先不考虑a为0的情况。

已知 \((p-x)^2\equiv x^2\pmod p\), 这是因为\(p^2-2xp+x^2\equiv x^2 \pmod p\)

所有有\(p-1\over 2\) 个不同的二次剩余,即\(1^2,2^2,\cdots,({p-1\over 2}) \pmod p\) (因为前一半和后一半相同了)

又\(a^{p-1}\equiv 1 \pmod p\), 可以写为\(({a^{p-1\over 2}-1})({a^{p-1\over 2}} + 1)\equiv 0\pmod p\)

上式中的前后两个因子,必须有一个为0,由\(Lagrangs's theorem\) 可知 k次多项式最多 k 个解,所以\(a^{p-1\over 2}-1\equiv 0\pmod p\) 有最多 \(p-1\over 2\)个解。

又\(x^2\equiv a\), 所以\(a^{p-1\over 2} \equiv (x^2)^{p-1\over 2} \equiv 1\pmod p\)

所以每一个平方剩余都可以使得第一个因子为0,非0平方剩余最少有\(p-1\over 2\)个,所以这与上面方程的解正好对应起来,也就是说使得\(a^{p-1\over 2}\equiv 1\)成立的\(a\) 都是\(p\) 的二次剩余。同理可知使得\(a^{p-1\over 2} \equiv -1\)成立的\(a\)都是\(p\)的非二次剩余。

2. 求解二次剩余

本题比较特殊,\(p\equiv 3\pmod 4\) ,那么根据上面推出来的

\[a^{p-1\over 2}\equiv1~\Rightarrow~ a^{p+1\over 2}\equiv x^2~\Rightarrow a^{p+1\over 4}\equiv x
\]

对于更一般的\(p\) , 可以参考: http://xbgjxt.swu.edu.cn/jsuns/html/jsuns/2019/1/201901009.htm

综述:

  1. 根据欧拉准则来判断是否有解
  2. 有解则求出原题中的x 与 y输出
  3. 无解则输出-1

标程代码:

int t, p = 1000000007;
//快速幂代码
long long pow(long long x, long long y, long long p) {
}
int main() {
cin >> t;
for (int tt = 0; tt < t; tt++) {
long long b, c;
cin >> b >> c;
long long d = (b * b - 4 * c) % p;
if (d < 0) {
d += p;
}
if (d != 0 && pow(d, (p - 1) / 2, p) != 1) {//如果无解,需要注意d为0的情况
cout << -1 << ' ' << -1 << endl;
} else {
long long r = pow(d, (p + 1) / 4, p);
long long x = (b + r) * pow(2, p - 2, p) % p;
long long y = (b - r) * pow(2, p - 2, p) % p;
if (x < 0)x += p;
if (y < 0)y += p;
if (x > y) swap(x, y);
cout << x << ' ' << y << endl;
}
}
}

自己的代码

const ll mod = 1e9+7;
ll pow_mod(ll a,ll i,ll n){
if(i == 0)return 1 % n;
ll tmp = pow_mod(a, i>> 1,n);
tmp = tmp * tmp % n;
if(i & 1)tmp = tmp * a % n;
return tmp;
}
//红宝书模板代码,求解模p下二次剩余为a的解
ll modsqr(ll a,ll n){
if(a == 0)return 0;
ll b,k,i,x;
if(n == 2)return a % n;
if(pow_mod(a,(n-1)/2,n) == 1){
if(n % 4 == 3)//本题中只会进入下面这个case
x = pow_mod(a,(n+1)/4,n);
else{
for(b = 1;pow_mod(b,(n-1)/2,n) == 1;b++);
i = (n-1)/2;
k = 0;
do{
i/=2;
k/=2;
if((pow_mod(a,i,n) * pow_mod(b,k,n) + 1) % n == 0)
k += (n-1) / 2;
}while(i % 2 == 0);
x = (pow_mod(a,(i+1)/2,n) * pow_mod(b,k/2,n))%n;
}
if(x * 2 > n)x = n-x;
return x;
}
return -1;
}
int main()
{
int T;cin>>T;
while(T--){
ll b,c;
scanf("%lld%lld",&b,&c);
ll q,a;
a = ((b*b - c * 4)%mod + mod) % mod;
q = modsqr(a,mod);
if(q == -1){//无解情况
puts("-1 -1");
continue;
}
//很蠢的分了两种情况....其实乘一个2的逆元即可
ll x = (q + b) / 2;
ll y = mod + b - x;
x = (x % mod + mod) % mod;
y = (y % mod + mod) % mod;
if((x * y) % mod == c){
if(x > y)swap(x,y);
printf("%lld %lld\n",x,y);continue;
}
x = (q + b + mod)/2;
y = b - x;
x = (x % mod + mod) % mod;
y = (y % mod + mod) % mod;
if((x * y) % mod == c){
if(x > y)swap(x,y);
printf("%lld %lld\n",x,y);continue;
}
puts("-1 -1");
}
return 0;
}

参考资料:

https://en.wikipedia.org/wiki/Quadratic_residue

https://en.wikipedia.org/wiki/Euler's_criterion

2019牛客暑期多校训练营(第九场)B Quadratic equation (平方剩余)的更多相关文章

  1. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  2. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  3. 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)

    layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...

  4. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  5. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  6. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  7. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  8. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  9. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  10. 2019牛客暑期多校训练营(第八场)E.Explorer

    链接:https://ac.nowcoder.com/acm/contest/888/E来源:牛客网 Gromah and LZR have entered the fifth level. Unli ...

随机推荐

  1. linux kernel操作GPIO函数

    一.头文件 #include <asm/gpio.h> 二.注册 GPIO int gpio_request(unsigned gpio, const char *label) 功能:申请 ...

  2. oracle 常用指令(持续更新中....)

    1. 查看所有表空间大小 select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_nam ...

  3. Ubuntu_Gedit配置

    Ubuntu_Gedit配置 为了换Ubuntu的时候能够更加方便,不用再用手重新打一遍代码,丢几个Gedit配置-- External Tools gdb compile (F2) #!/bin/s ...

  4. 【ORA】ORA-19602: cannot backup or copy active file in NOARCHIVELOG mode

    ORA-19602: cannot backup or copy active file in NOARCHIVELOG mode 这个问题是rman备份的时候,发现有问题 原因: 数据库没有开启归档 ...

  5. LOOP语句的AT语句块

    在loop一个内表的时候,如果想在loop循环中使用AT NEW ,AT END OF 等语句,一定需要注意的几点: 1.内表要排序 2.AT END OF 语句中影响的是指定字段前面所有的字段 3. ...

  6. 深度学习DeepLearning技术实战(12月18日---21日)

    12月线上课程报名中 深度学习DeepLearning(Python)实战培训班 时间地点: 2020 年 12 月 18 日-2020 年 12 月 21日 (第一天报到 授课三天:提前环境部署 电 ...

  7. Kubernetes 存储简介

    存储分类结构图 半持久化存储 1.EmptyDir EmptyDir是一个空目录,生命周期和所属的 Pod 是完全一致的,EmptyDir的用处是,可以在同一 Pod 内的不同容器之间共享工作过程中产 ...

  8. java实现Excel定制导出(基于POI的工具类)

    我的需求: 项目中有一些工程表格需要导出,设计到行列合并,定制样式,原有工具类冗余,内聚性强.所以想写一个可以随意定制excel的工具类,工具类满足需求: 对于常用的工程表格有模板格式,可以任意插拔. ...

  9. Flutter--Flutter中Widget、App的生命周期

    前言 在App的开发过程中,我们通常都需要了解App以及各个页面的生命周期,方便我们在App进入前台时启动一些任务,在进入后台后暂停一些任务.同时,各个页面的生命周期也很重要,每个页面消失时要做一些内 ...

  10. Jmeter的Cookie管理器调试与参数化

     默认系统都是需要登录,才能操作其它接口,所以需要添加一个HTTP Cookie 管理器,默认Cookie管理器是关闭的,需要修改jmeter配置文件jmeter.properties,该文件在jme ...