题意:求${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}$

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<iostream>
  6. #include<cmath>
  7. using namespace std;
  8. typedef long long ll;
  9. ll w,a,x,p,T;
  10. struct CN{
  11. ll x,y;
  12. CN friend operator *(CN x,CN y){
  13. CN z;
  14. z.x=(x.x*y.x+x.y*y.y*w)%p;
  15. z.y=(x.x*y.y+x.y*y.x)%p;
  16. return z;
  17. }
  18. };
  19.  
  20. CN Cmod_pow(CN x,ll n){
  21. CN z={,};
  22. while(n){
  23. if(n&)z=z*x;
  24. x=x*x;
  25. n>>=;
  26. }
  27. return z;
  28. }
  29.  
  30. ll mod_pow(ll x,ll n,ll p){
  31. ll res=;
  32. while(n){
  33. if(n&) res=res*x%p;
  34. x=x*x%p;
  35. n>>=;
  36. }
  37. return res;
  38. }
  39.  
  40. int main(){
  41. scanf("%lld",&T);
  42. while(T--){
  43. scanf("%lld%lld",&x,&p);
  44. x%=p;
  45. if(p==){
  46. printf("1\n");
  47. continue;
  48. }
  49. if(mod_pow(x,(p-)/,p)==p-){
  50. printf("No root\n");
  51. continue;
  52. }
  53. while(){
  54. a=rand()%p;
  55. w=(a*a-x+p)%p;
  56. if(mod_pow(w,(p-)/,p)==p-) break;
  57. }
  58.  
  59. CN u={a,};
  60. u=Cmod_pow(u,(p+)/);
  61. ll yi=u.x,er=p-u.x;
  62. if(yi>er) printf("%lld %lld\n",er,yi);
  63. else if(yi==er) printf("%lld\n",yi);
  64. else printf("%lld %lld\n",yi,er);
  65. }
  66. }

[ural1132]Square Root(cipolla算法)的更多相关文章

  1. 2018.12.17 ural1132 Square Root(二次剩余)

    传送门 MD写一道二次剩余的板题差点写自闭了. 我用的是cipollacipollacipolla算法. 利用的是欧拉准则来找寻一个二次非剩余类来求根. 注意这题有两个等根和模数为2的情况. 代码: ...

  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 ...

  3. Cipolla算法学习小记

    转自:http://blog.csdn.net/doyouseeman/article/details/52033204 简介 Cipolla算法是解决二次剩余强有力的工具,一个脑洞大开的算法. 认真 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. (Problem 57)Square root convergents

    It is possible to show that the square root of two can be expressed as an infinite continued fractio ...

  8. 二次剩余Cipolla算法学习笔记

    对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...

  9. Square Root

    Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm ...

随机推荐

  1. xpath取最后一个元素

    取xpath最后一个book元素 book[last()] 取xpath最后第二个book元素 book[last()-1]

  2. Elasticsearch使用记录

    Elasticsearch使用记录 Elasticsearch的搭建方法 1.RPM方式搭建 首先去官网[https://www.elastic.co/downloads/elasticsearch# ...

  3. Yii2学习笔记---内附GridView配置总结

    1./vendor/yiisoft/yii2/web/UrlManager.php 方法createUrl 修改url参数转码2.config/web.php 配置文件Yii::$app(应用主体)的 ...

  4. 浅谈Android系统开发中LOG的使用【转】

    本文转载自:http://blog.csdn.net/luoshengyang/article/details/6581828 在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以 ...

  5. html编辑器的调用

    <html><head>     <metahttp-equiv="Content-type"content="text/html; cha ...

  6. java入门了解12

    1.SequenceInputStream序列流:能将其他输入流的串联 用处:读完第一个再去读第二个输入流 用法:构造方法:SequenceInputStream(InputStream s1,Inp ...

  7. UOJ130 【NOI2015】荷马史诗

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. hadoop启动时解决权限问题

    解决以上问题,使用命令:sudo chown -hR jxh-ubuntu /usr/local/hadoop

  9. java:安装tomcat8/tomcat9(简单安装配置)

    java:安装tomcat8/tomcat9(简单安装配置) pache-tomcat-8.5.23(免安装板) 1.安装完成后右击我的电脑—属性—高级系统设置—环境变量, 在系统变量中添加以下变量 ...

  10. 分享知识-快乐自己:反射机制Demo解析

    Java-Reflect专题 基本反射简介: 1):JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象能够调用它的任意方法和属性;这种动态获取信息以及动 ...