题意:

求\(x^2 \equiv a \mod p\) 的所有整数解

思路:

二次剩余定理求解。

参考:

二次剩余Cipolla's algorithm学习笔记

板子:

//二次剩余,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(二次剩余定理)题解的更多相关文章

  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. Timus 1132 Square Root(二次剩余 解法2)

    不理解,背板子 #include<cstdio> using namespace std; int Pow(int a,int b,int p) { ; ) ) res=1LL*a*res ...

  3. 牛客多校第九场 && 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 ...

  4. 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 ...

  5. Ural 1001 - Reverse Root

    The problem is so easy, that the authors were lazy to write a statement for it! Input The input stre ...

  6. 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 ...

  7. 01背包 URAL 1073 Square Country

    题目传送门 /* 题意:问n最少能是几个数的平方和 01背包:j*j的土地买不买的问题 详细解释:http://www.cnblogs.com/vongang/archive/2011/10/07/2 ...

  8. URAL 1001 Reverse Root(水题?)

    The problem is so easy, that the authors were lazy to write a statement for it! Input The input stre ...

  9. 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 ...

随机推荐

  1. Python爬虫学习笔记(一)

    概念: 使用代码模拟用户,批量发送网络请求,批量获取数据. 分类: 通用爬虫: 通用爬虫是搜索引擎(Baidu.Google.Yahoo等)"抓取系统"的重要组成部分. 主要目的是 ...

  2. NOIP2020 T2 字符串匹配题解

    首先考虑O(n^3)的暴力怎么写. 显然,可以枚举字符串\(A\)+\(B\)的右端点,左端点显然是1,暴力判断是否能与后面的字符构成循环节,对于满足 \(k*(A+B)+C=\) 整个字符串\((k ...

  3. Django 模型(数据库)-cmd下的操作

    Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等数据库,只需要在settings.py ...

  4. jmeter报Address already in use: connect

    jmeter报Address already in use: connect   用windows进行jmeter压测出现java.net.BindException: Address already ...

  5. Property or method "previewUrl" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components,

    Property or method "previewUrl" is not defined on the instance but referenced during rende ...

  6. PHP 框架之一Laravel

    Laravel: Laravel The phpFramework for Web Artisans and one of the best php framework in year 2014. L ...

  7. # Set the asyncio reactor's event loop as global # TODO: Should we instead pass the global one into the reactor?

    daphne/server.py at master · django/daphne https://github.com/django/daphne/blob/master/daphne/serve ...

  8. 小鹏汽车技术中台实践 :微服务篇 InfoQ 今天 以下文章来源于InfoQ Pro

    小鹏汽车技术中台实践 :微服务篇 InfoQ  今天 以下文章来源于InfoQ Pro

  9. detect data races The cost of race detection varies by program, but for a typical program, memory usage may increase by 5-10x and execution time by 2-20x.

    小结: 1. conflicting access 2.性能危害 优化 The cost of race detection varies by program, but for a typical ...

  10. Springboot中mybatis控制台打印sql语句

    Springboot中mybatis控制台打印sql语句 https://www.jianshu.com/p/3cfe5f6e9174 https://www.jianshu.com/go-wild? ...