不理解,背板子

#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)的更多相关文章

  1. Timus 1132 Square Root(二次剩余)

    http://acm.timus.ru/problem.aspx?space=1&num=1132 题意: 求 x^2 ≡ n mod p  p是质数 的 解 本题中n>=1 特判p=2 ...

  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. 【BZOJ3132】【TYVJ1716】上帝造题的七分钟 二维树状数组

    题目大意 维护一个\(n\times m\)的矩阵,有两种操作: \(1~x_1~y_1~x_2~y_2~v\):把\((a,b),(c,d)\)为顶点的矩形区域内的所有数字加上\(v\). \(2~ ...

  2. BM算法

    BM算法 用来求解一个数列的递推式. 即给定\(\{x_i\}\)求解一个\(\{a_i\}\),满足\(|a|=m,x_n=\sum_{i=1}^ma_i*x_{n-i}\). 考虑增量法构造. 假 ...

  3. html图像、绝对路径和相对路径,链接

    html图像 <img>标签可以在网页上插入一张图片,它是独立使用的标签,通过"src"属性定义图片的地址,通过"alt"属性定义图片加载失败时显示 ...

  4. linux环境sed命令实例学习

    命令简介: sed(Stream Editor)被称作流编辑器.linux命令环境的“三剑客”(grep,sed,awk)之一,功能强大,可以根据命令来处理数据流中的数据,命令可以在命令行中,也可以出 ...

  5. FZU - 1901 Period II(kmp所有循环节)

    Problem Description For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SI ...

  6. java 判断元素是否在数组内

    一,先转为List,再使用contains()方法 String[] strArr = new String[] { "a", "b", "c&quo ...

  7. 第三篇-ubuntu18.04下截图快捷键

    ubuntu自带的截图工具感觉能够满足基本的截图功能,可以不必安装另外的截图软件. 一般用到的截图类型有三种:全屏.当前活动窗口.自定义区域,其中自定义区域截图是最灵活也是我们用的最多的方式.在ubu ...

  8. echarts柱状图 渐变色

    效果图:  var xAxisData = []; var data = []; for (var i = 9; i < 16; i++) { xAxisData.push('5月' + i + ...

  9. 查看android源码,windows环境下载源码

    查看源码 参考: http://blog.csdn.net/janronehoo/article/details/8560304 步骤: 添加chrome插件 Android SDK Search 进 ...

  10. java equals和hashcode方法

    equals()方法比较两个对象的引用是否相同 hashcode()方法比较两个对象的哈希码是否相同