This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/sqrt_mod
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include "src/Math/mod_sqrt.hpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while (T--) {
int Y, P;
cin >> Y >> P;
cout << mod_sqrt(Y, P) << '\n';
}
return 0;
}
#line 1 "test/yosupo/sqrt_mod.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/sqrt_mod
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#line 2 "src/Math/mod_sqrt.hpp"
#include <cassert>
#line 2 "src/Internal/Remainder.hpp"
namespace math_internal {
using namespace std;
using u8= unsigned char;
using u32= unsigned;
using i64= long long;
using u64= unsigned long long;
using u128= __uint128_t;
struct MP_Na { // mod < 2^32
u32 mod;
constexpr MP_Na(): mod(0) {}
constexpr MP_Na(u32 m): mod(m) {}
constexpr inline u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
constexpr inline u32 set(u32 n) const { return n; }
constexpr inline u32 get(u32 n) const { return n; }
constexpr inline u32 norm(u32 n) const { return n; }
constexpr inline u32 plus(u64 l, u32 r) const { return l+= r, l < mod ? l : l - mod; }
constexpr inline u32 diff(u64 l, u32 r) const { return l-= r, l >> 63 ? l + mod : l; }
};
template <class u_t, class du_t, u8 B> struct MP_Mo { // mod < 2^32, mod < 2^62
u_t mod;
constexpr MP_Mo(): mod(0), iv(0), r2(0) {}
constexpr MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
constexpr inline u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
constexpr inline u_t set(u_t n) const { return mul(n, r2); }
constexpr inline u_t get(u_t n) const { return n= reduce(n), n >= mod ? n - mod : n; }
constexpr inline u_t norm(u_t n) const { return n >= mod ? n - mod : n; }
constexpr inline u_t plus(u_t l, u_t r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
constexpr inline u_t diff(u_t l, u_t r) const { return l-= r, l >> (B - 1) ? l + (mod << 1) : l; }
private:
u_t iv, r2;
static constexpr u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
constexpr inline u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
using MP_Mo32= MP_Mo<u32, u64, 32>;
using MP_Mo64= MP_Mo<u64, u128, 64>;
struct MP_Br { // 2^20 < mod <= 2^41
u64 mod;
constexpr MP_Br(): mod(0), x(0) {}
constexpr MP_Br(u64 m): mod(m), x((u128(1) << 84) / m) {}
constexpr inline u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
static constexpr inline u64 set(u64 n) { return n; }
constexpr inline u64 get(u64 n) const { return n >= mod ? n - mod : n; }
constexpr inline u64 norm(u64 n) const { return n >= mod ? n - mod : n; }
constexpr inline u64 plus(u64 l, u64 r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
constexpr inline u64 diff(u64 l, u64 r) const { return l-= r, l >> 63 ? l + (mod << 1) : l; }
private:
u64 x;
constexpr inline u128 quo(const u128 &n) const { return (n * x) >> 84; }
constexpr inline u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
template <class du_t, u8 B> struct MP_D2B1 { // mod < 2^63, mod < 2^64
u64 mod;
constexpr MP_D2B1(): mod(0), s(0), d(0), v(0) {}
constexpr MP_D2B1(u64 m): mod(m), s(__builtin_clzll(m)), d(m << s), v(u128(-1) / d) {}
constexpr inline u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
constexpr inline u64 set(u64 n) const { return n; }
constexpr inline u64 get(u64 n) const { return n; }
constexpr inline u64 norm(u64 n) const { return n; }
constexpr inline u64 plus(du_t l, u64 r) const { return l+= r, l < mod ? l : l - mod; }
constexpr inline u64 diff(du_t l, u64 r) const { return l-= r, l >> B ? l + mod : l; }
private:
u8 s;
u64 d, v;
constexpr inline u64 rem(const u128 &u) const {
u128 q= (u >> 64) * v + u;
u64 r= u64(u) - (q >> 64) * d - d;
if (r > u64(q)) r+= d;
if (r >= d) r-= d;
return r;
}
};
using MP_D2B1_1= MP_D2B1<u64, 63>;
using MP_D2B1_2= MP_D2B1<u128, 127>;
template <class u_t, class MP> constexpr u_t pow(u_t x, u64 k, const MP &md) {
for (u_t ret= md.set(1);; x= md.mul(x, x))
if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;
}
}
#line 3 "src/NumberTheory/is_prime.hpp"
namespace math_internal {
template <class Uint, class MP, u32... args> constexpr bool miller_rabin(Uint n) {
const MP md(n);
const Uint s= __builtin_ctzll(n - 1), d= n >> s, one= md.set(1), n1= md.norm(md.set(n - 1));
for (u32 a: (u32[]){args...})
if (Uint b= a % n; b)
if (Uint p= md.norm(pow(md.set(b), d, md)); p != one)
for (int i= s; p != n1; p= md.norm(md.mul(p, p)))
if (!(--i)) return 0;
return 1;
}
}
constexpr bool is_prime(unsigned long long n) {
if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;
if (n < (1 << 30)) return math_internal::miller_rabin<unsigned, math_internal::MP_Mo32, 2, 7, 61>(n);
if (n < (1ull << 62)) return math_internal::miller_rabin<unsigned long long, math_internal::MP_Mo64, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
if (n < (1ull << 63)) return math_internal::miller_rabin<unsigned long long, math_internal::MP_D2B1_1, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
return math_internal::miller_rabin<unsigned long long, math_internal::MP_D2B1_2, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);
}
#line 4 "src/Math/mod_sqrt.hpp"
namespace math_internal {
template <class Int, class MP> constexpr i64 inner_sqrt(Int a, Int p) {
const MP md(p);
Int e= (p - 1) >> 1, one= md.set(1);
if (a= md.set(a); md.norm(pow(a, e, md)) != one) return -1;
Int b= 0, d= md.diff(0, a), ret= one, r2= 0, b2= one;
while (md.norm(pow(d, e, md)) == one) b= md.plus(b, one), d= md.diff(md.mul(b, b), a);
auto mult= [&md, d](Int &u1, Int &u2, Int v1, Int v2) {
Int tmp= md.plus(md.mul(u1, v1), md.mul(md.mul(u2, v2), d));
u2= md.plus(md.mul(u1, v2), md.mul(u2, v1)), u1= tmp;
};
for (++e;; mult(b, b2, b, b2)) {
if (e & 1) mult(ret, r2, b, b2);
if (!(e>>= 1)) return ret= md.get(ret), ret * 2 < p ? ret : p - ret;
}
}
}
constexpr long long mod_sqrt(long long a, long long p) {
assert(p > 0), assert(a >= 0), assert(is_prime(p)), a%= p;
if (a <= 1 || p == 2) return a;
if (p < (1 << 30)) return math_internal::inner_sqrt<unsigned, math_internal::MP_Mo32>(a, p);
if (p < (1ll << 62)) return math_internal::inner_sqrt<unsigned long long, math_internal::MP_Mo64>(a, p);
return math_internal::inner_sqrt<unsigned long long, math_internal::MP_D2B1_1>(a, p);
}
#line 6 "test/yosupo/sqrt_mod.test.cpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while (T--) {
int Y, P;
cin >> Y >> P;
cout << mod_sqrt(Y, P) << '\n';
}
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | example_00 |
![]() |
5 ms | 4 MB |
g++-13 | max_random_00 |
![]() |
66 ms | 4 MB |
g++-13 | max_random_01 |
![]() |
67 ms | 4 MB |
g++-13 | max_random_02 |
![]() |
68 ms | 4 MB |
g++-13 | max_random_03 |
![]() |
69 ms | 3 MB |
g++-13 | max_random_04 |
![]() |
68 ms | 3 MB |
g++-13 | mod_998244353_00 |
![]() |
92 ms | 4 MB |
g++-13 | mod_998244353_01 |
![]() |
92 ms | 4 MB |
g++-13 | random_00 |
![]() |
15 ms | 4 MB |
g++-13 | random_01 |
![]() |
16 ms | 4 MB |
g++-13 | random_02 |
![]() |
43 ms | 4 MB |
g++-13 | random_03 |
![]() |
21 ms | 4 MB |
g++-13 | random_04 |
![]() |
67 ms | 4 MB |
clang++-18 | example_00 |
![]() |
5 ms | 4 MB |
clang++-18 | max_random_00 |
![]() |
66 ms | 4 MB |
clang++-18 | max_random_01 |
![]() |
67 ms | 4 MB |
clang++-18 | max_random_02 |
![]() |
69 ms | 4 MB |
clang++-18 | max_random_03 |
![]() |
68 ms | 4 MB |
clang++-18 | max_random_04 |
![]() |
69 ms | 4 MB |
clang++-18 | mod_998244353_00 |
![]() |
92 ms | 4 MB |
clang++-18 | mod_998244353_01 |
![]() |
93 ms | 4 MB |
clang++-18 | random_00 |
![]() |
15 ms | 4 MB |
clang++-18 | random_01 |
![]() |
17 ms | 4 MB |
clang++-18 | random_02 |
![]() |
43 ms | 4 MB |
clang++-18 | random_03 |
![]() |
21 ms | 4 MB |
clang++-18 | random_04 |
![]() |
67 ms | 4 MB |