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 ...
随机推荐
- webapi Swagger 配置 services.BuildServiceProvider() 报警 ASP0000 问题处理
问题起源 网上的常见配置 Swagger 配置 在Startup类的 ConfigureServices 使用 services.BuildServiceProvider() ,其中有段代码如下: v ...
- 【高并发】ReadWriteLock怎么和缓存扯上关系了?!
写在前面 在实际工作中,有一种非常普遍的并发场景:那就是读多写少的场景.在这种场景下,为了优化程序的性能,我们经常使用缓存来提高应用的访问性能.因为缓存非常适合使用在读多写少的场景中.而在并发场景中, ...
- InnoDB的主键选择与插入优化
索引的存放方式MyISAM和InnoDB存储引擎在MySQL中,不同存储引擎对索引的实现方式是不同的,总结下MyISAM和InnoDB两个存储引擎的索引实现方式.MyISAM引擎使用B+Tree作为索 ...
- 【UNIAPP】接入导航系统完整版
// 查询附近/搜索关键词 <template> <view> <!--地图容器--> <map id="myMap" :markers= ...
- ftp上传文件出现553 Could not creat files 严重文件传输错误
之前上传文件到云服务器上一直出错,发现可以下载但是不能上传和编辑,后来终于找到原因了,是因为上传文件所在文件夹默认只有root用户才有写权限,所以我们还要将写权限赋予给其他用户.可以用Xshell 5 ...
- 使用eventfd创建一个用于事件通知的文件描述符
https://www.jianshu.com/p/57cc1d7d354f nat穿透代码c++
- 【LinuxShell】free 命令详解
前言 free命令用来显示Linux中的内存使用信息,包括空闲的.已用的物理内存,swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 命令格式 ...
- 【网络安全】IOC概念浅析
OpenIOC(Open Indicator of Compromise,开放威胁指标) MANDIANT 公司发布的情报共享规范,是开源.灵活的框架.OpenIOC是一个记录.定义以及共享威胁情报的 ...
- 2021年Web开发的7大趋势
技术发展日新月异,所以 Web 开发人员也需要及时了解行业最新的发展趋势. 全球有超过 17.4 亿个网站.在每一个细分领域都有无数企业争夺搜索引擎的排名前列位置.开发人员应该了解和发现更多创新的 W ...
- PowerBI数据建模时的交叉连接问题
方案一.在PowerPivot中,将其中一张表复制多份,分别与另一张表做链接. 方案二.在PowerQuery中,做多次合并查询,把所有数据集中在一张表中,方便后面的数据分析. 思考:不仅仅是在Pow ...