[ural1132]Square Root(cipolla算法)
题意:求${x^2} \equiv n\bmod p$
解题关键:
定理:若$a$满足$w = {a^2} - n$是模$p$的二次非剩余,即,${x^2} = w\bmod p$无解,则${(a + \sqrt w )^{\frac{{p + 1}}{2}}}$是二次剩余方程${x^2} \equiv n\bmod p$的解。
证明:
$\begin{array}{l}
{x^2} \equiv {(a + \sqrt w )^{p + 1}} \equiv (a + \sqrt w ){(a + \sqrt w )^p}\\
\equiv (a + \sqrt w )(\sum {C_p^i{a^{p - i}}{w^{\frac{i}{2}}}} )\\
\equiv (a + \sqrt w )({a^p} + {w^{\frac{{p - 1}}{2}}}\sqrt w )\\
\equiv (a + \sqrt w )(a - \sqrt w )\\
\equiv n(\bmod p)
\end{array}$
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<cstdlib>
- #include<iostream>
- #include<cmath>
- using namespace std;
- typedef long long ll;
- ll w,a,x,p,T;
- struct CN{
- ll x,y;
- CN friend operator *(CN x,CN y){
- CN z;
- z.x=(x.x*y.x+x.y*y.y*w)%p;
- z.y=(x.x*y.y+x.y*y.x)%p;
- return z;
- }
- };
- CN Cmod_pow(CN x,ll n){
- CN z={,};
- while(n){
- if(n&)z=z*x;
- x=x*x;
- n>>=;
- }
- return z;
- }
- ll mod_pow(ll x,ll n,ll p){
- ll res=;
- while(n){
- if(n&) res=res*x%p;
- x=x*x%p;
- n>>=;
- }
- return res;
- }
- int main(){
- scanf("%lld",&T);
- while(T--){
- scanf("%lld%lld",&x,&p);
- x%=p;
- if(p==){
- printf("1\n");
- continue;
- }
- if(mod_pow(x,(p-)/,p)==p-){
- printf("No root\n");
- continue;
- }
- while(){
- a=rand()%p;
- w=(a*a-x+p)%p;
- if(mod_pow(w,(p-)/,p)==p-) break;
- }
- CN u={a,};
- u=Cmod_pow(u,(p+)/);
- ll yi=u.x,er=p-u.x;
- if(yi>er) printf("%lld %lld\n",er,yi);
- else if(yi==er) printf("%lld\n",yi);
- else printf("%lld %lld\n",yi,er);
- }
- }
[ural1132]Square Root(cipolla算法)的更多相关文章
- 2018.12.17 ural1132 Square Root(二次剩余)
传送门 MD写一道二次剩余的板题差点写自闭了. 我用的是cipollacipollacipolla算法. 利用的是欧拉准则来找寻一个二次非剩余类来求根. 注意这题有两个等根和模数为2的情况. 代码: ...
- Project Euler 80:Square root digital expansion 平方根数字展开
Square root digital expansion It is well known that if the square root of a natural number is not an ...
- Cipolla算法学习小记
转自:http://blog.csdn.net/doyouseeman/article/details/52033204 简介 Cipolla算法是解决二次剩余强有力的工具,一个脑洞大开的算法. 认真 ...
- Codeforces 715A. Plus and Square Root[数学构造]
A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 612E - Square Root of Permutation
E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...
- Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- (Problem 57)Square root convergents
It is possible to show that the square root of two can be expressed as an infinite continued fractio ...
- 二次剩余Cipolla算法学习笔记
对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...
- Square Root
Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm ...
随机推荐
- xpath取最后一个元素
取xpath最后一个book元素 book[last()] 取xpath最后第二个book元素 book[last()-1]
- Elasticsearch使用记录
Elasticsearch使用记录 Elasticsearch的搭建方法 1.RPM方式搭建 首先去官网[https://www.elastic.co/downloads/elasticsearch# ...
- Yii2学习笔记---内附GridView配置总结
1./vendor/yiisoft/yii2/web/UrlManager.php 方法createUrl 修改url参数转码2.config/web.php 配置文件Yii::$app(应用主体)的 ...
- 浅谈Android系统开发中LOG的使用【转】
本文转载自:http://blog.csdn.net/luoshengyang/article/details/6581828 在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以 ...
- html编辑器的调用
<html><head> <metahttp-equiv="Content-type"content="text/html; cha ...
- java入门了解12
1.SequenceInputStream序列流:能将其他输入流的串联 用处:读完第一个再去读第二个输入流 用法:构造方法:SequenceInputStream(InputStream s1,Inp ...
- UOJ130 【NOI2015】荷马史诗
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- hadoop启动时解决权限问题
解决以上问题,使用命令:sudo chown -hR jxh-ubuntu /usr/local/hadoop
- java:安装tomcat8/tomcat9(简单安装配置)
java:安装tomcat8/tomcat9(简单安装配置) pache-tomcat-8.5.23(免安装板) 1.安装完成后右击我的电脑—属性—高级系统设置—环境变量, 在系统变量中添加以下变量 ...
- 分享知识-快乐自己:反射机制Demo解析
Java-Reflect专题 基本反射简介: 1):JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象能够调用它的任意方法和属性;这种动态获取信息以及动 ...