Timus 1132 Square Root(二次剩余)
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(二次剩余)的更多相关文章
- Timus 1132 Square Root(二次剩余 解法2)
不理解,背板子 #include<cstdio> using namespace std; int Pow(int a,int b,int p) { ; ) ) res=1LL*a*res ...
- URAL 1132 Square Root(二次剩余定理)题解
题意: 求\(x^2 \equiv a \mod p\) 的所有整数解 思路: 二次剩余定理求解. 参考: 二次剩余Cipolla's algorithm学习笔记 板子: //二次剩余,p是奇质数 l ...
- 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 ...
- 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 ...
- 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 ...
- Square Root
Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm ...
- 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 ...
随机推荐
- sql里的正则表达式
SQL语句还可以搭配正则表达式作为查询条件,很是有用. REGEXP_LIKE(匹配)REGEXP_INSTR (包含)REGEXP_REPLACE(替换)REGEXP_SUBSTR(提取) 表 1: ...
- Android 简单天气预报
IDE: Android studio3.1.2 界面: activity_main.xml
- 【hdu 6161】Big binary tree(二叉树、dp)
多校9 1001 hdu 6161 Big binary tree 题意 有一个完全二叉树.编号i的点值是i,操作1是修改一个点的值为x,操作2是查询经过点u的所有路径的路径和最大值.10^5个点,1 ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- W10笔记本电脑弄成WIFI
用网线连接的笔记本弄成WIFI供手机上网.以前是买了一个随身 WIFI,可以当网卡使用.后来使用命令 // 建立WIFI netsh wlan set hostednetwork mode=allow ...
- Nginx+Keepalived部署
-----------ReProxy-------------------------Client-----------192.168.56.200 nginx+keepalived 192.168. ...
- poj3179 Corral the Cows
论水题与难题的差距:在于一个upper_bound 那么,这题一看就很显然了:因为答案满足二分性质所以我们二分. 然后我们再建造一个二维前缀和,每次判断的时候怎么办呢? 我先以为是贪心:选择以每个点为 ...
- spring MVC 如何接收前台传入的JSON对象数组并处理
spring MVC 如何接收前台传入的JSON对象数组 主要方法: (主要用到的包是 net.sf.json 即:json-lib-2.3-jdk15.jar 完整相关jar包: commons- ...
- NOIP 普及组 2012 寻宝(思维???)
传送门 https://www.cnblogs.com/violet-acmer/p/9937201.html 题解: 一开始用暴力查找下一个要去的房间,超时了,emmmmm....... 然后,就稍 ...
- grub.conf解析
一.grub简介系统启动引导管理器,是在计算机启动后运行的第一个程序,他是用来负责加载.传输控制到操作系统的内核,一旦把内核挂载,系统引导管理器的任务就算完成退出,系统引导的其它部份,比如系统的初始化 ...