题意:求${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. Vue mixins extends extend components

    mixins 调用方式: mixins: [mixin1, mixin2] 是对父组件的扩充,包括methods.components.directive等... 触发钩子函数时,先调用mixins的 ...

  2. explain分析sql效果

    1.id:  代表select 语句的编号, 如果是连接查询,表之间是平等关系, select 编号都是1,从1开始. 如果某select中有子查询,则编号递增.如下一条语句2个结果 mysql> ...

  3. 算法(Algorithms)第4版 练习 2.2.26

    在sort函数创建aux数组: package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.alg ...

  4. linux学习系列一

    1. 基本命令(注意参数的大小写) 学习linux如果使用的是windows 建议使用一个很好用的工具git,下载安装即可使用linux下的命令来操作windows 1.1目录及文件 注意/ 有表示根 ...

  5. DIV+CSS IE6/IE7/IE8/FF兼容问题大全

    1. [代码][CSS]代码 1, FF下给 div 设置 padding 后会导致 width 和 height 增加, 但IE不会.(可用!important解决) 2, 居中问题. 1).垂直居 ...

  6. CSS3分享功能

    [代码][CSS]代码  $.fn.share = function(opts) {  var $body, $head;  if ($(this).length === 0) {    consol ...

  7. window.showModalDialog()之返回值

    window.showModalDialog的基本用法 showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.show ...

  8. 一篇 jQuery 常用方法及函数的文章留存备忘。

    jQuery 常见操作实现方式 $("标签名") //取html元素 document.getElementsByTagName("") $("#ID ...

  9. java - BigDecimal的format()方法和setScale()方法格式字符串

    1.BigDecimal.setScale()方法用于格式化小数点 setScale(1)表示保留一位小数,默认用四舍五入方式 setScale(1,BigDecimal.ROUND_DOWN)直接删 ...

  10. 关于c++中命名空间namespace

    一.定义命名空间: 步骤一:在.h文件中:namespace  ns{.......}//将定义的类和全局变量,全局函数写入花括号内. 步骤二:在.cpp文件中: using namespace ns ...