题意:求${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算法)的更多相关文章

  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. JDK线程池的实现

    线程池 接口Executor 该接口只有一个方法,JDK解释如下 执行已提交的Runnable 任务的对象.此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节.调度等)分离开来的方 ...

  2. Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals

    http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...

  3. RabbitMQ点对点的模式

    1.点对点模式  一对一模式.  一个生产者投递消息给队列 只能允许有一个消费者进行消费    如果集群的话 会进行均摊消费   服务器配置不一样 均摊就不优了 长连接 不用三次握手之类的 提高传输效 ...

  4. Spark- Spark内核架构原理和Spark架构深度剖析

    Spark内核架构原理 1.Driver 选spark节点之一,提交我们编写的spark程序,开启一个Driver进程,执行我们的Application应用程序,也就是我们自己编写的代码.Driver ...

  5. cocos2d-x 中 xml 文件读取

    实现类 CXmlParse 啥也不说了  直接上硬货  believe yourself   一看就明白 CXmlParse.h #ifndef __C_XML_PARSE__ #define __C ...

  6. JNI简易入门

    JNI简介 JNI(Java Native Interface)是JDK的一部分,提供了若干API实现了Java和其他语言的通信(主要是C/C++).JNI主要用于以下场景: 贴近硬件底层的功能,Ja ...

  7. 【JVM】java棧

    java棧和函数调用的关系图 [名词解释]--->java棧是一块线程的私有空间--->java的棧是先进后出的数据结构.函数返回,则该函数的棧帧被弹出.--->一个函数对应一个棧帧 ...

  8. XML的二十个热点问题

    这些日子,几乎每个人都在谈论XML (Extensible Markup Language),但是很少有人真正理解其含义.XML的推崇者认为它能够解决所有HTML不能解决的问题,让数据在不同的操作系统 ...

  9. [转]Unity3D学习笔记(四)天空、光晕和迷雾

    原文地址:http://bbs.9ria.com/thread-186942-1-1.html 作者:江湖风云 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的 ...

  10. MFC中如何不使用Unicode字符集

    命令窗口:调试->属性-> 把字符集设置为:未设置