URAL 1132 Square Root(二次剩余定理)题解
题意:
求\(x^2 \equiv a \mod p\) 的所有整数解
思路:
二次剩余定理求解。
参考:
板子:
//二次剩余,p是奇质数
ll ppow(ll a, ll b, ll mod){
ll ret = 1;
a = a % mod;
while(b){
if(b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
struct TT{
ll p, d;
};
ll w;
TT mul_er(TT a, TT b, ll mod){
TT ans;
ans.p = (a.p * b.p % mod + a.d * b.d % mod * w % mod) % mod;
ans.d = (a.p * b.d % mod + a.d * b.p % mod) % mod;
return ans;
}
TT power(TT a, ll b, ll mod){
TT ret;
ret.p = 1, ret.d = 0;
while(b){
if(b & 1) ret = mul_er(ret, a, mod);
a = mul_er(a, a, mod);
b >>= 1;
}
return ret;
}
ll legendre(ll a, ll p){
return ppow(a, (p - 1) >> 1, p);
}
ll modulo(ll a, ll mod){
a %= mod;
if(a < 0) a += mod;
return a;
}
ll solve(ll n, ll p){ //x^2 = n mod p
if(n == 0) return 0;
if(n == 1) return 1;
if(p == 2) return 1;
if(legendre(n, p) + 1 == p) return -1; //无解
ll a = -1, t;
while(true){
a = rand() % p;
t = a * a - n;
w = modulo(t, p);
if(legendre(w, p) + 1 == p) break;
}
TT temp;
temp.p = a;
temp.d = 1;
TT ans = power(temp, (p + 1) >> 1, p);
return ans.p;
}
代码:
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 5e4 + 5;
const int INF = 0x3f3f3f3f;
const ull seed = 131;
const ll MOD = 1e9 + 7;
using namespace std;
//二次剩余
ll ppow(ll a, ll b, ll mod){
ll ret = 1;
a = a % mod;
while(b){
if(b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
struct TT{
ll p, d;
};
ll w;
TT mul_er(TT a, TT b, ll mod){
TT ans;
ans.p = (a.p * b.p % mod + a.d * b.d % mod * w % mod) % mod;
ans.d = (a.p * b.d % mod + a.d * b.p % mod) % mod;
return ans;
}
TT power(TT a, ll b, ll mod){
TT ret;
ret.p = 1, ret.d = 0;
while(b){
if(b & 1) ret = mul_er(ret, a, mod);
a = mul_er(a, a, mod);
b >>= 1;
}
return ret;
}
ll legendre(ll a, ll p){
return ppow(a, (p - 1) >> 1, p);
}
ll modulo(ll a, ll mod){
a %= mod;
if(a < 0) a += mod;
return a;
}
ll solve(ll n, ll p){ //x^2 = n mod p
if(n == 0) return 0;
if(n == 1) return 1;
if(p == 2) return 1;
if(legendre(n, p) + 1 == p) return -1; //无解
ll a = -1, t;
while(true){
a = rand() % p;
t = a * a - n;
w = modulo(t, p);
if(legendre(w, p) + 1 == p) break;
}
TT temp;
temp.p = a;
temp.d = 1;
TT ans = power(temp, (p + 1) >> 1, p);
return ans.p;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
ll a, n;
scanf("%lld%lld", &a, &n);
ll ans1 = solve(a, n), ans2;
if(ans1 == -1){
printf("No root\n");
continue;
}
ans2 = n - ans1;
if(ans1 > ans2) swap(ans1, ans2);
if(ans1 == ans2) printf("%lld\n", ans1);
else printf("%lld %lld\n", ans1, ans2);
}
return 0;
}
URAL 1132 Square Root(二次剩余定理)题解的更多相关文章
- Timus 1132 Square Root(二次剩余)
http://acm.timus.ru/problem.aspx?space=1&num=1132 题意: 求 x^2 ≡ n mod p p是质数 的 解 本题中n>=1 特判p=2 ...
- Timus 1132 Square Root(二次剩余 解法2)
不理解,背板子 #include<cstdio> using namespace std; int Pow(int a,int b,int p) { ; ) ) res=1LL*a*res ...
- 牛客多校第九场 && ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解
题意1.1: 求\(\sum_{i=1}^n Fib^m\mod 1e9+9\),\(n\in[1, 1e9], m\in[1, 1e4]\) 思路1.1 我们首先需要知道斐波那契数列的通项是:\(F ...
- 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 ...
- Ural 1001 - Reverse Root
The problem is so easy, that the authors were lazy to write a statement for it! Input The input stre ...
- 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 ...
- 01背包 URAL 1073 Square Country
题目传送门 /* 题意:问n最少能是几个数的平方和 01背包:j*j的土地买不买的问题 详细解释:http://www.cnblogs.com/vongang/archive/2011/10/07/2 ...
- URAL 1001 Reverse Root(水题?)
The problem is so easy, that the authors were lazy to write a statement for it! Input The input stre ...
- 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 ...
随机推荐
- 远程部署项目,修改catalina.bat文件 完美解决在代理服务器上HttpURLConnection 调接口超时的问题
远程给客户部署项目,运行时程序调外部接口时总是出不去,经过不懈努力,后来发现客户那边的网络走的是代理,于是在代码中加下面代码: //设置代理 System.setProperty("http ...
- es6语法详解
什么是ECMAScript? ECMAScript是浏览器脚本语言的规范,而我们熟知的js语言,如JavaScript则是规范的具体实现.es6就好比Java的jdk. 一.es6语法详解:let声明 ...
- .NET Core 问题记录
前言: 最近在项目中遇到了遇到了写部署步骤过多的问题,为了减少.net core项目部署步骤:需要对一些基础问题进行验证: 如端口设置.单页应用程序(angluar)合并部署方式等相关问题,特将解决过 ...
- 虚拟化kvm的搭建
虚拟化, 是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机 ,在一台计算机上同时运行多个逻辑计算机,每台逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互不 ...
- 前端面试之HTTP状态码!
前端面试之HTTP协议的东西! 一次HTTP请求的流程! HTTP 状态码 成功响应(200–299) 状态码 含义 200 请求成功 201 该请求已成功,并因此创建了一个新的资源.这通常是在POS ...
- mysql事务测试
mysql事务测试 打开mysql的命令行,将自动提交事务给关闭 --查看是否是自动提交 1表示开启,0表示关闭 select @@autocommit; --设置关闭 set autocommit ...
- 从零开始学Java (五)条件选择
if switch while do while for break continue 这块对于有语言基础的人来说可以跳过了. 注意有个equals方法. 1 public class Main { ...
- tcp的3次握手4次挥手
- JPEG解码——(3)文件头解析
与具体的编码数据空间相比,jpeg文件头占据非常小乃至可以忽略不计的大小. 仍然拿JPEG解码--(1)JPEG文件格式概览中的<animal park>这张图片来举例,从跳过SOS(FF ...
- ResponseEntity和@ResponseBody以及@ResponseStatus区别
看的迷迷糊糊的 https://www.jdon.com/springboot/responseentity.html