不理解,背板子

#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. 【BZOJ3601】一个人的数论(数论)

    [BZOJ3601]一个人的数论(数论) 题面 BZOJ 怎么这图片这么大啊... 题解 要求的是\(\displaystyle \sum_{i=1}^n [gcd(i,n)=1]i^d\) 然后把\ ...

  2. 【博弈论】浅谈泛Nim游戏

    Nim游戏在ACM中碰到了,就拎出来写写. 一般Nim游戏:有n堆石子,每堆石子有$a_i$个,每次可以取每堆石子中$[0,a_i-1]$,问先手是否有必胜策略. 泛Nim游戏:每堆石子有$a_i$个 ...

  3. Codeforces | CF1041F 【Ray in the tube】

    昨天晚上全机房集体开\(Div2\),因为人傻挂两次\(B\)题的我开场就\(rank2000+\dots qwq\)于是慌乱之中的我就开始胡乱看题(口胡),于是看了\(F\dots\)(全机房似乎也 ...

  4. js-基本语法

    条件语句 通过条件来控制程序的走向,就需要用到条件语句. 运算符 1.算术运算符: +(加). -(减). (乘). /(除). %(求余) 2.赋值运算符:=. +=. -=. =. /=. %= ...

  5. python 验证码识别

    一.python识别简单验证码: 代码: ''' func:实现简单验证码获取 ''' import pytesseract from PIL import Image #首先通过Image打开一个图 ...

  6. Electron入门笔记(二)-快速建立hello world

    官方的文档我没有看懂,看了不少别人的博客和文章,终于慢慢看懂了如何快速的建立一个Electron app demo,前一篇文章不是使用官方快速搭建的,而且还出了小问题,所以去撸了一遍quick-sta ...

  7. sshd启动报错Could not load host key

    ~ # /usr/sbin/sshd Could not load host key: /etc/ssh/ssh_host_rsa_key Could not load host key: /etc/ ...

  8. 洛谷P3620 数据备份

    好吧,我一开始说这是个神级数据结构毒瘤题,后来改成神题了. 主要是贪心做法的巧妙转化: 首先发现选择的一对必须相邻,于是我们搞出差分. 然后考虑选取最小值时,最小值两侧的数要么同时选,要么都不选. 然 ...

  9. 洛谷P2414 阿狸的打字机

    题意:以trie的形式给出n个字符串,每次询问第x个字符串在第y个字符串中出现了几次. 解:总串长是n2级别的,所以不能用什么后缀自动机... [update]可以建triesam但是不知道trie上 ...

  10. matplotlib 将两张数据视图在一起显示

    import numpy as np import pandas as pd from matplotlib import pyplot as plt if __name__ == "__m ...