[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 ...
随机推荐
- Java进阶学习:JSON解析利器JackSon
Java:JSON解析利器JackSon JackSon基础 1.Maven项目引入 <!-- https://mvnrepository.com/artifact/org.codehaus.j ...
- 【leetcode】Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- wget 实现web监控脚本
#!/bin/sbin timeout= times= url=https://1.1.1.1 while true;do wget --no-check-certificate --timeout= ...
- CSS整体布局
主要内容: 一.外边距margin与填充padding 二.浮动float与显示display 三.主布局 四.定位方式posotion 一.外边距margin与填充padding 1.margin设 ...
- mysql 的类型转换函数cast的用法
CAST(expr AS type), CONVERT(expr,type) , CONVERT(expr USING transcoding_name) CAST() ...
- 模拟RPG格斗
三个英雄角色参与PK 每个英雄具有以下几个属性:生命值(为0时英雄倒下).攻击力(每次攻击时扣除对方的生命值点数).攻击间隔(每次攻击过后都要等待间隔时间才能进行下次攻击,首次攻击之前也要先等待间隔时 ...
- Web前端理论知识记录
Web前端理论知识记录 Elena· 5 个月前 cookies,sessionStorage和localStorage的区别? sessionStorage用于本地存储一个会话(session) ...
- Hibernate错误及解决办法
1.Hibernate 报错:this project is not a myeclipse hibernate project . assuming hibernate 3 cap res:项目名上 ...
- IIS 高并发导致log记录不完全
项目测试性能过程中,对于高并发测试过程中发现log记录缺失一部分,经过调查,找到了原因是因为IIS连接数的限制,经过修改连接数,成功完成.设置如下: “点击网站”->“右击切换到功能视图”-&g ...
- Python基础-redis模块使用
redis是一个数据库,他的数据全都是存放在内存里面的,redis每秒能支持30w次的读写,存放有两种格式,一种string类型,一种是hash类型 一,操作string类型 r=redis.Redi ...