Timus 1132 Square Root(二次剩余 解法2)
不理解,背板子
#include<cstdio> using namespace std; int Pow(int a,int b,int p)
{
int res=;
for(;b;a=1LL*a*a%p,b>>=)
if(b&) res=1LL*a*res%p;
return res;
} bool Legendre(int a,int p)
{
return Pow(a,p->>,p)==;
} void modsqr(int a,int p)
{
int x;
int i,k,b;
if(p==) x=a%p;
else if(p%==) x=Pow(a,p+>>,p);
else
{
for(b=;Legendre(b,p);++b);
i=p->>;
k=;
do
{
i>>=;
k>>=;
if(!((1LL*Pow(a,i,p)*Pow(b,k,p)+)%p)) k+=p->>;
}while(!(i&));
x=1LL*Pow(a,i+>>,p)*Pow(b,k>>,p)%p;
}
if(p-x<x) x=p-x;
if(x==p-x) printf("%d\n",x);
else printf("%d %d\n",x,p-x);
} int main()
{
freopen("data.txt","r",stdin);
freopen("aa.txt","w",stdout);
int T;
scanf("%d",&T);
int a,n;
while(T--)
{
scanf("%d%d",&a,&n);
a%=n;
if(!Legendre(a,n))
{
puts("No root");
continue;
}
modsqr(a,n);
}
return ;
}
Timus 1132 Square Root(二次剩余 解法2)的更多相关文章
- Timus 1132 Square Root(二次剩余)
http://acm.timus.ru/problem.aspx?space=1&num=1132 题意: 求 x^2 ≡ n mod p p是质数 的 解 本题中n>=1 特判p=2 ...
- 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 ...
随机推荐
- 【XSY1529】小Q与进位制 分治 FFT
题目大意 小Q发明了一种进位制,每一位的变化范围是\(0\)~\(b_i-1\),给你一个这种进位制下的整数\(a\),问你有多少非负整数小于\(a\).结果以十进制表示. \(n\leq 1 ...
- Hdoj 1856.More is better 题解
Problem Description Mr Wang wants some boys to help him with a project. Because the project is rathe ...
- [复习]动态dp
[复习]动态dp 你还是可以认为我原来写的动态dp就是在扯蛋. [Luogu4719][模板]动态dp 首先作为一个\(dp\)题,我们显然可以每次修改之后都进行暴力\(dp\),设\(f[i][0/ ...
- 洛谷 P1129 [ZJOI2007]矩阵游戏 解题报告
P1129 [ZJOI2007]矩阵游戏 题目描述 小\(Q\)是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个\(N*N\)黑白方阵进行(如同国际象棋一般 ...
- HR_Two Strings
https://www.hackerrank.com/challenges/two-strings/problem?h_l=interview&playlist_slugs%5B%5D=int ...
- Vagrant 遇到的问题
Vagrant was unable to mount VirtualBox shared folders. This is usuallybecause the filesystem "v ...
- electron入门笔记(三)- 引入bootstrap
源码:https://github.com/sueRimn/electron-bootstrap 当引入jQuery和bootstrap文件时,会报错,原因是:electron 的 Renderer ...
- poj3190 Stall Reservations
我一开始想用线段树,但是发现还要记录每头cow所在的棚...... 无奈之下选择正解:贪心. 用priority_queue来维护所有牛棚中结束时间最早的那个牛棚,即可得出答案. 注意代码实现的细节. ...
- CF341E Candies Game
题目链接 题意 有\(n\)个盒子,第\(i\)个盒子里面有\(a_i\)个糖果.每次选择两个盒子\(i,j\),假设\(a_i \le a_j\).然后从第\(j\)个盒子中拿出\(a_i\)个糖果 ...
- 第十三节、SURF特征提取算法
上一节我们已经介绍了SIFT算法,SIFT算法对旋转.尺度缩放.亮度变化等保持不变性,对视角变换.仿射变化.噪声也保持一定程度的稳定性,是一种非常优秀的局部特征描述算法.但是其实时性相对不高. SUR ...