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. bzoj 1029: [JSOI2007]建筑抢修 (优先队列)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1029 思路: 按结束时间排序,优先选结束时间短的,选完后扔到优先队列里(大的优先),如果选到 ...

  2. wstngfw 初始化的一些配置

    wstngfw 初始化的一些配置 1. 引导界面 2. 命令行菜单界面 3. Assign Interfaces (分配接口) Should VLANs be set up now [y|n]? nW ...

  3. WCF 改成 restful api

    1.  右健 svc , view markup, 添加 Factory="System.ServiceModel.Activation.WebServiceHostFactory" ...

  4. EasyFlash 的初始化配置

    @2019-02-18 [小记] EasyFlash的初始化流程 easyflash_init ---> ef_port_init ---> sfud_init ---> sfud_ ...

  5. vue自定义插件-弹框

    <template> <transition name="msgbox"> <div v-if="show" class=&quo ...

  6. [2017-7-28]Android Learning Day7

    View动画效果 透明动画效果 旋转动画效果 移动动画效果 缩放动画效果 混合动画效果 1.透明动画效果(AlphaAnimation) 有两种方法 第一种在活动中设置,不需要xml文件 public ...

  7. luogu5020 [NOIp2018]货币系统 (完全背包)

    我那个新的货币系统,就是把原来的货币系统中能被其他数表示的数删掉 那我就算有多少数能被别的数表示,那肯定是要被比它小的表示 于是排个序做完全背包就好了 但是我太zz不会完全背包,然后写了个bitset ...

  8. nio再学习之通道channel

    通道(Channel):用于在数据传输过程中,进行输入输出的通道,其与(流)Stream不一样,流是单向的,在BIO中我们分为输入流,输出流,但是在通道中其又具有读的功能也具有写的功能或者两者同时进行 ...

  9. linux复制文件到一个不存在的文件夹

    复制文件到一个不存在的文件夹时,会报错 cp -f aaa /home/admin/.m2/cp: 无法创建普通文件"/home/admin/.m2/": 是一个目录 解决的方式: ...

  10. 把axios封装为vue插件使用

    前言 自从Vue2.0推荐大家使用 axios 开始,axios 被越来越多的人所了解.使用axios发起一个请求对大家来说是比较简单的事情,但是axios没有进行封装复用,项目越来越大,引起的代码冗 ...