Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
mr.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <cstdint>
35
36namespace console {
37
38#ifdef __SIZEOF_INT128__
47 inline uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t mod) noexcept {
48 return (unsigned __int128)a * b % mod;
49 }
50#else
60 inline uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t mod) noexcept {
61 uint64_t result = 0;
62 a %= mod;
63 b %= mod;
64 while (b > 0) {
65 if (b & 1) result = (result + a) % mod;
66 a = (a * 2) % mod;
67 b >>= 1;
68 }
69 return result;
70 }
71#endif
72
81 inline uint64_t mod_pow(uint64_t a, uint64_t b, uint64_t mod) noexcept {
82 uint64_t result = 1;
83 a %= mod;
84 while (b > 0) {
85 if (b & 1) result = mod_mul(result, a, mod);
86 a = mod_mul(a, a, mod);
87 b >>= 1;
88 }
89 return result;
90 }
91
100 inline bool miller_rabin_test(uint64_t n, uint64_t a) noexcept {
101 if (n < 2) return false;
102 if (n == 2 || n == 3) return true;
103 if (n % 2 == 0) return false;
104 uint64_t d = n - 1;
105 int s = 0;
106 while (d % 2 == 0) {
107 d /= 2;
108 s++;
109 }
110 uint64_t x = mod_pow(a, d, n);
111 if (x == 1 || x == n - 1) return true;
112 for (int r = 1; r < s; r++) {
113 x = mod_mul(x, x, n);
114 if (x == n - 1) return true;
115 if (x == 1) return false;
116 }
117 return false;
118 }
119
129 inline bool is_prime(uint64_t n) noexcept {
130 if (n < 2) return false;
131 static const uint64_t small_primes[]
132 = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
133 static const uint64_t bases[] = {2ULL,
134 325ULL,
135 9375ULL,
136 28178ULL,
137 450775ULL,
138 9780504ULL,
139 1795265022ULL};
140 for (uint64_t p : small_primes) {
141 if (n % p == 0) return n == p;
142 }
143 for (uint64_t a : bases) {
144 if (a >= n) continue;
145 if (!miller_rabin_test(n, a)) return false;
146 }
147 return true;
148 }
149}
Mod< T > mod(T v)
创建取模固定值变换器。
Definition gen.h:2093
本库所有组件所在的顶层命名空间。
bool miller_rabin_test(uint64_t n, uint64_t a) noexcept
单次 Miller-Rabin 素性测试。
Definition mr.h:100
uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t mod) noexcept
使用二进制加法模拟模乘。
Definition mr.h:60
bool is_prime(uint64_t n) noexcept
确定性素数判定。
Definition mr.h:129
uint64_t mod_pow(uint64_t a, uint64_t b, uint64_t mod) noexcept
模幂运算。
Definition mr.h:81