\((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. git的使用命令总结

    git一般方法git init 本地目录生成仓库git clone +项目url地址 克隆远程仓库git status 查看状态git pull 拉取 拉取的代码在用户家目录下git push 上传g ...

  2. linux中常用服务的安装

    安装环境:centos7.5 配置离线yum源参考:https://blog.csdn.net/mayh554024289/article/details/54236336vi /etc/yum.co ...

  3. maven 报的一堆错

    今天初学maven,刚开始下载的是Apache-maven-3.6.2然后配置运行一个servlet,但是在pom.xml中写jar包坐标时一直报错显示红色,本地仓库和官网上的中央仓库都试过了就是依赖 ...

  4. 十六:SQL注入之查询方式及报错盲注

    在很多注入时,有很多注入会出现无回显的情况,其中不回显的原因可能是SQL查询语句有问题,这时候我们需要用到相关的报错或者盲注进行后续操作,同时作为手工注入的时候,需要提前了解SQL语句能更好的选择对应 ...

  5. docker 报错: Cannot connect to the Docker daemon at unix:///var/run/docker.sock.

    最近在 Windows 子系统 WSL 上面安装了一个 ubuntu18.04, 安装完docker 跑 hello-world 的时候报错了 docker: Cannot connect to th ...

  6. 【Spring】 Spring的核心容器

    Spring的核心容器 文章目录 Spring的核心容器 BeanFactory ApplicationContext 1.通过ClassPathXmlApplicationContext创建 2.通 ...

  7. JAVA之JDBC数据库连接池总结篇

    JDBC数据库连接池 一.JDBC数据库连接池的必要性 二.数据库连接池技术 三.多种开源的数据库连接池 3.1 C3P0数据库连接池 3.2 DBCP数据库连接池 3.3 Druid(德鲁伊)数据库 ...

  8. 【RAC】安装cluster软件 在节点2执行root.sh脚本

    安装cluster软件  在节点2执行root.sh脚本 报错如下: Running vipca(silent) for configuring nodeapps /db/oracle/product ...

  9. LeetCode671. 二叉树中第二小的节点

    题目 纯暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 int findSecondMinimumValue(TreeNode* r ...

  10. ctfshow—web—web7

    打开靶机 发现是SQL注入,盲注 过滤了空格符,可以用/**/绕过,抓包 直接上脚本 import requestss=requests.session()url='https://46a0f98e- ...