http://acm.timus.ru/problem.aspx?space=1&num=1132

题意:

求 x^2 ≡ n mod p  p是质数 的 解

本题中n>=1

特判p=2,接下来求当p是奇素数时的解

引理1:

引理2:方程有解当且仅当

定理:

设a满足 不是模p的二次剩余,

无解,

那么是二次剩余方程的解

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7. typedef long long LL;
  8.  
  9. int w;
  10.  
  11. struct T
  12. {
  13. int p,d;
  14. };
  15.  
  16. int mod(LL a,int p)
  17. {
  18. a%=p;
  19. if(a<) a+=p;
  20. return a;
  21. }
  22.  
  23. int Pow(int a,int b,int p)
  24. {
  25. int res=;
  26. for(;b;a=1LL*a*a%p,b>>=)
  27. if(b&) res=1LL*res*a%p;
  28. return res;
  29. }
  30.  
  31. //求勒让德符号
  32. int Legendre(int a,int p)
  33. {
  34. return Pow(a,p->>,p);
  35. }
  36.  
  37. //二次域上的乘法
  38. T mul(T a,T b,int p)
  39. {
  40. T ans;
  41. ans.p=(1LL*a.p*b.p%p+1LL*a.d*b.d%p*w%p)%p;
  42. ans.d=(1LL*a.p*b.d%p+1LL*a.d*b.p%p)%p;
  43. return ans;
  44. }
  45.  
  46. //二次域上的快速幂
  47. T power(T a,int b,int p)
  48. {
  49. T ans;
  50. ans.p=;
  51. ans.d=;
  52. for(;b;a=mul(a,a,p),b>>=)
  53. if(b&) ans=mul(ans,a,p);
  54. return ans;
  55. }
  56.  
  57. int solve(int n,int p)
  58. {
  59. if(p==) return ;
  60. if(Legendre(n,p)+==p) return -;
  61. int a;
  62. LL t;
  63. while()
  64. {
  65. a=rand()%p;
  66. t=1LL*a*a-n;
  67. w=mod(t,p);
  68. if(Legendre(w,p)+==p) break;
  69. }
  70. T tmp;
  71. tmp.p=a;
  72. tmp.d=;
  73. T ans=power(tmp,p+>>,p);
  74. return ans.p;
  75. }
  76.  
  77. int main()
  78. {
  79. int t;
  80. scanf("%d",&t);
  81. int n,p;
  82. int a,b;
  83. while(t--)
  84. {
  85. scanf("%d%d",&n,&p);
  86. n%=p;
  87. a=solve(n,p);
  88. if(a==-)
  89. {
  90. puts("No root");
  91. continue;
  92. }
  93. b=p-a;
  94. if(a>b) swap(a,b);
  95. if(a==b) printf("%d\n",a);
  96. else printf("%d %d\n",a,b);
  97. }
  98. }

Timus 1132 Square Root(二次剩余)的更多相关文章

  1. Timus 1132 Square Root(二次剩余 解法2)

    不理解,背板子 #include<cstdio> using namespace std; int Pow(int a,int b,int p) { ; ) ) res=1LL*a*res ...

  2. URAL 1132 Square Root(二次剩余定理)题解

    题意: 求\(x^2 \equiv a \mod p\) 的所有整数解 思路: 二次剩余定理求解. 参考: 二次剩余Cipolla's algorithm学习笔记 板子: //二次剩余,p是奇质数 l ...

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

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

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

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

  9. Codeforces Round #372 (Div. 1) A. Plus and Square Root 数学题

    A. Plus and Square Root 题目连接: http://codeforces.com/contest/715/problem/A Description ZS the Coder i ...

随机推荐

  1. MT【284】构造函数的导数的两类题型

    第一类: 已知定义在$R$上的奇函数$f(x),f(-1)=0,$当$x>0$时,$xf^{'}(x)-f(x)<0,$则$f(x)>0$的解集为____ 第二类: 已知函数$f(x ...

  2. python学习日记(函数进阶)

    命名空间 内置命名空间 存放了python解释器为我们提供的名字:print,input...等等,他们都是我们熟悉的,拿过来就可以用的方法. 内置的名字在启动解释器(程序运行前)的时候被加载在内存里 ...

  3. Haproxy 优化

    Haproxy 自身健康检查vi /usr/local/haproxy/sbin/check_haproxy.sh #!/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/b ...

  4. Hdoj 1115.Lifting the Stone 题解

    Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...

  5. LOJ#2302 整数

    解:发现这苟东西是个3千万位的二进制数......毒瘤吧. 拆位考虑,如果一个地方本来是1然后+1,就会把它和它前面连续的一段1变成0,并把第一个0变成1. 如果本来是0然后-1了,就会把它和它前面连 ...

  6. poj1637 Sightseeing tour(混合图欧拉回路)

    题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...

  7. [hdu6183][Color it]

    题目链接 题目大意 有一个矩阵,总共有4种操作 0:清空这个矩阵 1 x y c:将\((x,y)(1 \leq x ,y\leq 10^6)\)这个点加上一种颜色c\((0\leq c \leq 5 ...

  8. netsh interface portproxy的一个简单例子

    netsh interface portproxy的微软帮助文档地址: https://technet.microsoft.com/zh-cn/library/cc776297(WS.10).aspx ...

  9. Dreamweaver - <!DOCTYPE html>

    最近设计网页,很多使用 <!DOCTYPE html> 关于<!DOCTYPE html>的详细介绍: http://www.w3school.com.cn/tags/tag_ ...

  10. 解决win10环境下python Selenuim调用Chrome时提示data 及Chrome正在受自动软件控制的方法

    用python自动访问谷歌浏览器时会出现data界面,很是烦人.在网上搜索,有说是因为webdriver和google版本不匹配导致的,就下过各种版本,结果都一样. 后来明白了,出现data的原因只是 ...