From ba1ce4bd76ee5353fa72524a3dab5e1c8f8614d8 Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov Date: Mon, 7 Oct 2024 01:32:28 +0200 Subject: [PATCH] Remove extra number_theory.hpp --- cp-algo/math/number_theory.hpp | 71 ---------------------------------- 1 file changed, 71 deletions(-) delete mode 100644 cp-algo/math/number_theory.hpp diff --git a/cp-algo/math/number_theory.hpp b/cp-algo/math/number_theory.hpp deleted file mode 100644 index 0508ea1..0000000 --- a/cp-algo/math/number_theory.hpp +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef CP_ALGO_MATH_NUMBER_THEORY_HPP -#define CP_ALGO_MATH_NUMBER_THEORY_HPP -#include "cp-algo/random/rng.hpp" -#include "number_theory/modint.hpp" -#include "affine.hpp" -#include -#include -#include -#include -namespace cp_algo::math { - // Find min non-negative x s.t. a*b^x = c (mod m) - std::optional discrete_log(int64_t b, int64_t c, uint64_t m, int64_t a = 1) { - if(std::abs(a - c) % m == 0) { - return 0; - } - if(std::gcd(a, m) != std::gcd(a * b, m)) { - auto res = discrete_log(b, c, m, a * b % m); - return res ? std::optional(*res + 1) : res; - } - // a * b^x is periodic here - using base = dynamic_modint; - return base::with_mod(m, [&]() -> std::optional { - size_t sqrtmod = std::max(1, std::sqrt(m) / 2); - std::unordered_map small; - base cur = a; - for(size_t i = 0; i < sqrtmod; i++) { - small[cur.getr()] = i; - cur *= b; - } - base step = bpow(base(b), sqrtmod); - cur = 1; - for(size_t k = 0; k < m; k += sqrtmod) { - auto it = small.find((base(c) * cur).getr()); - if(it != end(small)) { - auto cand = base::with_mod(period(base(b)), [&](){ - return base(it->second - k); - }).getr(); - if(base(a) * bpow(base(b), cand) == base(c)) { - return cand; - } else { - return std::nullopt; - } - } - cur *= step; - } - return std::nullopt; - }); - } - // https://en.wikipedia.org/wiki/Berlekamp-Rabin_algorithm - template - std::optional sqrt(base b) { - if(b == base(0)) { - return base(0); - } else if(bpow(b, (b.mod() - 1) / 2) != base(1)) { - return std::nullopt; - } else { - while(true) { - base z = random::rng(); - if(z * z == b) { - return z; - } - lin x(1, z, b); // x + z (mod x^2 - b) - x = bpow(x, (b.mod() - 1) / 2, lin(0, 1, b)); - if(x.a != base(0)) { - return x.a.inv(); - } - } - } - } -} -#endif // CP_ALGO_MATH_NUMBER_THEORY_HPP