2019牛客暑期多校训练营(第九场)B Quadratic equation (平方剩余)
\((x+y)\equiv b\pmod p\)
\((x\times y)\equiv c\pmod p\)
由第一个式子可知:\(x+y=b~or~x+y=b+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\)
\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\) ,那么根据上面推出来的
\]
对于更一般的\(p\) , 可以参考: http://xbgjxt.swu.edu.cn/jsuns/html/jsuns/2019/1/201901009.htm
综述:
- 根据欧拉准则来判断是否有解
- 有解则求出原题中的x 与 y输出
- 无解则输出-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 (平方剩余)的更多相关文章
- 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem
题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3 4 2 3 4 输出:0 0 1 题解: 认真想一 ...
- 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)
题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem
链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第八场)E.Explorer
链接:https://ac.nowcoder.com/acm/contest/888/E来源:牛客网 Gromah and LZR have entered the fifth level. Unli ...
随机推荐
- Java 并发编程要点
使用线程 有三种使用线程的方法: 实现 Runnable 接口: 实现 Callable 接口: 继承 Thread 类. 实现 Runnable 和 Callable 接口的类只能当做一个可以在线程 ...
- python函数3-函数嵌套/递归/匿名函数
2 .函数递归: 3.匿名函数
- 【Zabbix】配置zabbix agent向多个server发送数据
1.背景: server端: 172.16.59.197 ,172.16.59.98 agent 端: hostname:dba-test-hzj02 IP:172.16.59.98 2.方式: 配 ...
- ORACLE查找占用临时表空间多的SESSION
需要使用SYS用户登录查看 /* Formatted on 2020/12/30 上午 11:17:12 (QP5 v5.163.1008.3004) */ SELECT k.inst_id &quo ...
- powershell中的cmdlet命令
Add-Computer 向域或工作组中添加计算机. Add-Content 向指定的项中添加内容,如向文件中添加字词. Add-History 向会话历史记录追加条目. Add-Member 向 W ...
- Ribbon负载均衡服务调用
1.在听周阳老师讲解时,使用Ribbon核心组件IRule时是这样用的: ribbon版本 : 自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,项目结构如下 MySelfR ...
- 24V降压3.3V芯片,低压降线性稳压器
PW6206系列是一款高精度,高输入电压,低静态电流,高速,低压降线性稳压器具有高纹波抑制.在VOUT=5V&VIN=7V时,输入电压高达40V,负载电流高达300mA,采用BCD工艺制造.P ...
- 前端知识(一)02 初识 Node.js-谷粒学院
目录 初识Node.js 一.Node.js的概念 1.JavaScript引擎 2.什么是Node.js 3.Node.js有什么用 二.BFF 1.BFF 解决什么问题 2.BFF是什么 三.安装 ...
- apscheduler(定时任务) 基于redis持久化配置操作
apscheduler(定时任务) 基于redis持久化配置操作 安装模块 pip install apscheduler 导入模块配置 ## 配置redis模块 from apscheduler.j ...
- Linux安装MYSQL并部署主从复制集群
主节点部署 安装数据库 Ubuntu apt-get install mysql-server -y systemctl start mysql systemctl enabled mysql Cen ...