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的二次剩余,

无解,

那么是二次剩余方程的解

#include<cstdio>
#include<cstdlib>
#include<algorithm> using namespace std; typedef long long LL; int w; struct T
{
int p,d;
}; int mod(LL a,int p)
{
a%=p;
if(a<) a+=p;
return a;
} int Pow(int a,int b,int p)
{
int res=;
for(;b;a=1LL*a*a%p,b>>=)
if(b&) res=1LL*res*a%p;
return res;
} //求勒让德符号
int Legendre(int a,int p)
{
return Pow(a,p->>,p);
} //二次域上的乘法
T mul(T a,T b,int p)
{
T ans;
ans.p=(1LL*a.p*b.p%p+1LL*a.d*b.d%p*w%p)%p;
ans.d=(1LL*a.p*b.d%p+1LL*a.d*b.p%p)%p;
return ans;
} //二次域上的快速幂
T power(T a,int b,int p)
{
T ans;
ans.p=;
ans.d=;
for(;b;a=mul(a,a,p),b>>=)
if(b&) ans=mul(ans,a,p);
return ans;
} int solve(int n,int p)
{
if(p==) return ;
if(Legendre(n,p)+==p) return -;
int a;
LL t;
while()
{
a=rand()%p;
t=1LL*a*a-n;
w=mod(t,p);
if(Legendre(w,p)+==p) break;
}
T tmp;
tmp.p=a;
tmp.d=;
T ans=power(tmp,p+>>,p);
return ans.p;
} int main()
{
int t;
scanf("%d",&t);
int n,p;
int a,b;
while(t--)
{
scanf("%d%d",&n,&p);
n%=p;
a=solve(n,p);
if(a==-)
{
puts("No root");
continue;
}
b=p-a;
if(a>b) swap(a,b);
if(a==b) printf("%d\n",a);
else printf("%d %d\n",a,b);
}
}

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. [ZJOI2009]函数 题解

    题目链接:[ZJOI2009]函数 对于$n=1$的情况,直接输出$1$ 对于$n>1$的情况,由于我们可以将图上下反转,所以第$k$层的情况可以被转成第$n-k+1$层 规律自己打个表可以推出 ...

  2. 【BZOJ4126】【BZOJ3516】【BZOJ3157】国王奇遇记 线性插值

    题目描述 三倍经验题. 给你\(n,m\),求 \[ \sum_{i=1}^ni^mm^i \] \(n\leq {10}^9,1\leq m\leq 500000\) 题解 当\(m=1\)时\(a ...

  3. WC2019 tree

    WC2019唯一一道正常的题,考场上没什么想法,也只拿到了暴力分.搞了一天终于做完了. 前置知识:purfer序,多项式exp或分治FTT. 对于\(type=0\)的,随便维护下,算下联通块即可. ...

  4. Java抽象类简单学习

    使用抽象类应该注意的几个要点: 包含一个或者多个抽象方法的类必须被声明为抽象类. 将类声明为抽象类,不一定含有抽象方法. 通常认为,在抽象类中不应该包括具体方法,建议尽量将通用的域和方法放在超类中. ...

  5. numpy ndarray求其最值的索引

    import numpy as np a = np.array([1,2,3,4]) np.where(a== np.max(a)) >>>3 但假设其最值不止一个,如下 a = n ...

  6. JIRA和Confluence更改JVM内存大小解决访问打开缓慢问题

    原因: 根据主机物理内存不同,默认的java虚拟机内存也会不同(一个较低值),有时候不够用,可以修改默认设置,改善内存不足导致的问题. 操作步骤: JIRA 1. 打开至相应目录: ~]# cd  / ...

  7. vue使用element-ui 监听使用回车键事件

    因为element-ui 对input做了封装,使用@keyup.enter="fn"触发Enter键事件就不会触发,解决:后面追加.native.@keyup.enter.nat ...

  8. linux less对文件内容进行搜索

    [ 可以先用 less 文件名 来打开文件, 然后可以按回车,打开底部命令输入行(即出现一个冒号的位置), 然后可以使用  键盘上的 home 键跳到文件开始,end键跳到最后,PgUp向前翻页,Pg ...

  9. echarts如何给柱形图的每个柱子设置不同颜色

    总结下这几日用echarts库作基本图形遇到的一些问题. echarts快速上手可参考官网: http://echarts.baidu.com/tutorial.html#5%20%E5%88%86% ...

  10. 新建工程时报错(26, 13) Failed to resolve: com.android.support:appcompat-v7:28.+ ,

    allprojects { repositories { jcenter() maven { url "https://maven.google.com" } } }