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 ...
随机推荐
- MT【284】构造函数的导数的两类题型
第一类: 已知定义在$R$上的奇函数$f(x),f(-1)=0,$当$x>0$时,$xf^{'}(x)-f(x)<0,$则$f(x)>0$的解集为____ 第二类: 已知函数$f(x ...
- python学习日记(函数进阶)
命名空间 内置命名空间 存放了python解释器为我们提供的名字:print,input...等等,他们都是我们熟悉的,拿过来就可以用的方法. 内置的名字在启动解释器(程序运行前)的时候被加载在内存里 ...
- Haproxy 优化
Haproxy 自身健康检查vi /usr/local/haproxy/sbin/check_haproxy.sh #!/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/b ...
- Hdoj 1115.Lifting the Stone 题解
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- LOJ#2302 整数
解:发现这苟东西是个3千万位的二进制数......毒瘤吧. 拆位考虑,如果一个地方本来是1然后+1,就会把它和它前面连续的一段1变成0,并把第一个0变成1. 如果本来是0然后-1了,就会把它和它前面连 ...
- poj1637 Sightseeing tour(混合图欧拉回路)
题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...
- [hdu6183][Color it]
题目链接 题目大意 有一个矩阵,总共有4种操作 0:清空这个矩阵 1 x y c:将\((x,y)(1 \leq x ,y\leq 10^6)\)这个点加上一种颜色c\((0\leq c \leq 5 ...
- netsh interface portproxy的一个简单例子
netsh interface portproxy的微软帮助文档地址: https://technet.microsoft.com/zh-cn/library/cc776297(WS.10).aspx ...
- Dreamweaver - <!DOCTYPE html>
最近设计网页,很多使用 <!DOCTYPE html> 关于<!DOCTYPE html>的详细介绍: http://www.w3school.com.cn/tags/tag_ ...
- 解决win10环境下python Selenuim调用Chrome时提示data 及Chrome正在受自动软件控制的方法
用python自动访问谷歌浏览器时会出现data界面,很是烦人.在网上搜索,有说是因为webdriver和google版本不匹配导致的,就下过各种版本,结果都一样. 后来明白了,出现data的原因只是 ...