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 ...
随机推荐
- Python执行程序实可视化_heartrate
最近发现了一个Python程序执行的简单实时可视化神器,名字叫 heartrate,安装完运行可以看到下面这样的炫酷过程. 虽然很炫酷,但有点看不懂. 来解释下,左边的动态数字代表每行被触发的次数.变 ...
- VGA调试心得
以前自己调试过视频信号,无非就时钟加行场同步加数据线,如果视频信号出问题,第一看现象,第二测频率,反正出问题不是消隐信号出问题,就是时钟频率出问题.通过这种方式也调试成功过几个显示屏,然后就以为自己对 ...
- dotnet cli 5.0 新特性——dotnet tool search
dotnet cli 5.0 新特性--dotnet tool search Intro .NET 5.0 SDK 的发布,给 dotnet cli 引入了一个新的特性,dotnet tool sea ...
- 洛谷P4127
Description 给出两个数 \(a\),\(b\) ,求出 \([a,b]\) 中各位数字之和能整除原数的数的个数 Solution 设 \(f[i][j][k][q]\) 表示 枚举到第 i ...
- Java中的异常处理机制《》
异常机制已经成为判断一门编程语言是否成熟的标准,异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅,并提高程序健壮性. Java异常机制主要依赖于try.catch.finall ...
- PL/SQL 学习分享(续)
事务 事务的概述 事务的特性 回滚点 事务实例练习 动态SQL 动态SQL概述 动态SQL应用场合 动态SQL的执行语法 绑定变量 动态SQL创建表 动态SQL绑定变量 动态SQL综合案例添加数据 使 ...
- Spark获取DataFrame中列的方式--col,$,column,apply
Spark获取DataFrame中列的方式--col,$,column,apply 1.官方说明 2.使用时涉及到的的包 3.Demo 原文作者:大葱拌豆腐 原文地址:Spark获取DataFrame ...
- GIS基本概念,空间分析
GIS基本概念,空间分析 一.GIS基本概念 1.1 要素模型(Feature) 1.2 矢量数据 1.3 空间分析 1.3.1 空间查询和空间量算 1.3.2 缓冲区分析 1.3.3 叠加分析 1. ...
- 删除Kafka中的topic
删除Kafka中的topic 一.配置delete.topic.enable=true 二.其他方法 一.配置delete.topic.enable=true 修改kafaka配置文件server.p ...
- Spring boot获取getBean
package com.job.center.quartz.common; import org.springframework.beans.BeansException; import org.sp ...