|
| 1 | +--- |
| 2 | +data: |
| 3 | + _extendedDependsOn: |
| 4 | + - icon: ':heavy_check_mark:' |
| 5 | + path: weilycoder/number-theory/modint.hpp |
| 6 | + title: Modular Integer Arithmetic Utilities |
| 7 | + - icon: ':heavy_check_mark:' |
| 8 | + path: weilycoder/number-theory/prime.hpp |
| 9 | + title: Prime Number Utilities |
| 10 | + _extendedRequiredBy: [] |
| 11 | + _extendedVerifiedWith: [] |
| 12 | + _isVerificationFailed: false |
| 13 | + _pathExtension: cpp |
| 14 | + _verificationStatusIcon: ':heavy_check_mark:' |
| 15 | + attributes: |
| 16 | + '*NOT_SPECIAL_COMMENTS*': '' |
| 17 | + PROBLEM: https://judge.yosupo.jp/problem/primality_test |
| 18 | + links: |
| 19 | + - https://judge.yosupo.jp/problem/primality_test |
| 20 | + bundledCode: "#line 1 \"test/primality_test.test.cpp\"\n#define PROBLEM \"https://judge.yosupo.jp/problem/primality_test\"\ |
| 21 | + \n\n#line 1 \"weilycoder/number-theory/prime.hpp\"\n\n\n\n/**\n * @file prime.hpp\n\ |
| 22 | + \ * @brief Prime Number Utilities\n */\n\n#line 1 \"weilycoder/number-theory/modint.hpp\"\ |
| 23 | + \n\n\n\n#include <cstdint>\n\n/**\n * @file modint.hpp\n * @brief Modular Integer\ |
| 24 | + \ Arithmetic Utilities\n */\n\nnamespace weilycoder {\n/**\n * @brief Perform\ |
| 25 | + \ modular multiplication for 64-bit integers.\n * @tparam bit32 If true, won't\ |
| 26 | + \ use 128-bit arithmetic. You should ensure that\n * all inputs are small\ |
| 27 | + \ enough to avoid overflow (i.e. bit-32).\n * @param a The first multiplicand.\n\ |
| 28 | + \ * @param b The second multiplicand.\n * @param modulus The modulus.\n * @return\ |
| 29 | + \ (a * b) % modulus\n */\ntemplate <bool bit32 = false>\nuint64_t modular_multiply_64(uint64_t\ |
| 30 | + \ a, uint64_t b, uint64_t modulus) {\n if constexpr (bit32)\n return a * b\ |
| 31 | + \ % modulus;\n else\n return static_cast<unsigned __int128>(a) * b % modulus;\n\ |
| 32 | + }\n\n/**\n * @brief Perform modular exponentiation for 64-bit integers.\n * @tparam\ |
| 33 | + \ bit32 If true, won't use 128-bit arithmetic. You should ensure that\n * \ |
| 34 | + \ all inputs are small enough to avoid overflow (i.e. bit-32).\n * @param\ |
| 35 | + \ base The base number.\n * @param exponent The exponent.\n * @param modulus The\ |
| 36 | + \ modulus.\n * @return (base^exponent) % modulus\n */\ntemplate <bool bit32 =\ |
| 37 | + \ false>\nconstexpr uint64_t fast_power_64(uint64_t base, uint64_t exponent, uint64_t\ |
| 38 | + \ modulus) {\n uint64_t result = 1 % modulus;\n base %= modulus;\n while (exponent\ |
| 39 | + \ > 0) {\n if (exponent & 1)\n result = modular_multiply_64<bit32>(result,\ |
| 40 | + \ base, modulus);\n base = modular_multiply_64<bit32>(base, base, modulus);\n\ |
| 41 | + \ exponent >>= 1;\n }\n return result;\n}\n} // namespace weilycoder\n\n\n\ |
| 42 | + #line 11 \"weilycoder/number-theory/prime.hpp\"\n#include <type_traits>\n\nnamespace\ |
| 43 | + \ weilycoder {\n/**\n * @brief Miller-Rabin primality test for a given base.\n\ |
| 44 | + \ * @tparam bit32 If true, won't use 128-bit arithmetic. You should ensure that\n\ |
| 45 | + \ * all inputs are small enough to avoid overflow (i.e. bit-32).\n * @tparam\ |
| 46 | + \ base The base to test.\n * @param n The number to test for primality.\n * @param\ |
| 47 | + \ d An odd component of n-1 (n-1 = d * 2^s).\n * @param s The exponent of 2 in\ |
| 48 | + \ the factorization of n-1.\n * @return true if n is probably prime for the given\ |
| 49 | + \ base, false if composite.\n */\ntemplate <bool bit32, uint64_t base>\nconstexpr\ |
| 50 | + \ bool miller_rabin_test(uint64_t n, uint64_t d, uint32_t s) {\n uint64_t x =\ |
| 51 | + \ fast_power_64<bit32>(base, d, n);\n if (x == 0 || x == 1 || x == n - 1)\n \ |
| 52 | + \ return true;\n for (uint32_t r = 1; r < s; ++r) {\n x = modular_multiply_64<bit32>(x,\ |
| 53 | + \ x, n);\n if (x == n - 1)\n return true;\n }\n return false;\n}\n\n\ |
| 54 | + /**\n * @brief Variadic template to test multiple bases in Miller-Rabin test.\n\ |
| 55 | + \ * @tparam bit32 If true, won't use 128-bit arithmetic. You should ensure that\n\ |
| 56 | + \ * all inputs are small enough to avoid overflow (i.e. bit-32).\n * @tparam\ |
| 57 | + \ base The first base to test.\n * @tparam Rest The remaining bases to test.\n\ |
| 58 | + \ * @param n The number to test for primality.\n * @param d An odd component of\ |
| 59 | + \ n-1 (n-1 = d * 2^s).\n * @param s The exponent of 2 in the factorization of\ |
| 60 | + \ n-1.\n * @return true if n is probably prime for all given bases, false if composite.\n\ |
| 61 | + \ */\ntemplate <bool bit32, uint64_t base, uint64_t... Rest>\nconstexpr std::enable_if_t<(sizeof...(Rest)\ |
| 62 | + \ != 0), bool>\nmiller_rabin_test(uint64_t n, uint64_t d, uint32_t s) {\n return\ |
| 63 | + \ miller_rabin_test<bit32, base>(n, d, s) &&\n miller_rabin_test<bit32,\ |
| 64 | + \ Rest...>(n, d, s);\n}\n\n/**\n * @brief Miller-Rabin primality test using multiple\ |
| 65 | + \ bases.\n * @tparam bit32 If true, won't use 128-bit arithmetic. You should ensure\ |
| 66 | + \ that\n * all inputs are small enough to avoid overflow (i.e. bit-32).\n\ |
| 67 | + \ * @tparam bases The bases to test.\n * @param n The number to test for primality.\n\ |
| 68 | + \ * @return true if n is probably prime, false if composite.\n */\ntemplate <bool\ |
| 69 | + \ bit32, uint64_t... bases> constexpr bool miller_rabin(uint64_t n) {\n if (n\ |
| 70 | + \ < 2)\n return false;\n if (n == 2 || n == 3)\n return true;\n if (n\ |
| 71 | + \ % 2 == 0)\n return false;\n\n uint64_t d = n - 1, s = 0;\n for (; d % 2\ |
| 72 | + \ == 0; d /= 2)\n ++s;\n\n return miller_rabin_test<bit32, bases...>(n, d,\ |
| 73 | + \ s);\n}\n\n/**\n * @brief Miller-Rabin primality test optimized for 64-bit integers.\n\ |
| 74 | + \ * Uses a fixed set of bases that guarantee correctness\n * for\ |
| 75 | + \ 64-bit integers.\n * @param n The number to test for primality.\n * @return\ |
| 76 | + \ true if n is prime, false if not prime.\n */\nconstexpr bool miller_rabin64(uint64_t\ |
| 77 | + \ n) {\n return miller_rabin<false, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);\n\ |
| 78 | + }\n\n/**\n * @brief Miller-Rabin primality test optimized for 32-bit integers.\n\ |
| 79 | + \ * Uses a fixed set of bases that guarantee correctness\n * for\ |
| 80 | + \ 32-bit integers.\n * @param n The number to test for primality.\n * @return\ |
| 81 | + \ true if n is prime, false if not prime.\n */\nconstexpr bool miller_rabin32(uint32_t\ |
| 82 | + \ n) { return miller_rabin<true, 2, 7, 61>(n); }\n} // namespace weilycoder\n\n\ |
| 83 | + \n#line 5 \"test/primality_test.test.cpp\"\n#include <iostream>\nusing namespace\ |
| 84 | + \ std;\nusing namespace weilycoder;\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n\ |
| 85 | + \ cin.exceptions(cin.failbit | cin.badbit);\n size_t q;\n cin >> q;\n while\ |
| 86 | + \ (q--) {\n uint64_t n;\n cin >> n;\n if (n <= UINT32_MAX)\n cout\ |
| 87 | + \ << (miller_rabin32(n) ? \"Yes\\n\" : \"No\\n\");\n else\n cout << (miller_rabin64(n)\ |
| 88 | + \ ? \"Yes\\n\" : \"No\\n\");\n }\n return 0;\n}\n" |
| 89 | + code: "#define PROBLEM \"https://judge.yosupo.jp/problem/primality_test\"\n\n#include\ |
| 90 | + \ \"../weilycoder/number-theory/prime.hpp\"\n#include <cstdint>\n#include <iostream>\n\ |
| 91 | + using namespace std;\nusing namespace weilycoder;\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n\ |
| 92 | + \ cin.exceptions(cin.failbit | cin.badbit);\n size_t q;\n cin >> q;\n while\ |
| 93 | + \ (q--) {\n uint64_t n;\n cin >> n;\n if (n <= UINT32_MAX)\n cout\ |
| 94 | + \ << (miller_rabin32(n) ? \"Yes\\n\" : \"No\\n\");\n else\n cout << (miller_rabin64(n)\ |
| 95 | + \ ? \"Yes\\n\" : \"No\\n\");\n }\n return 0;\n}" |
| 96 | + dependsOn: |
| 97 | + - weilycoder/number-theory/prime.hpp |
| 98 | + - weilycoder/number-theory/modint.hpp |
| 99 | + isVerificationFile: true |
| 100 | + path: test/primality_test.test.cpp |
| 101 | + requiredBy: [] |
| 102 | + timestamp: '2025-10-31 09:52:58+08:00' |
| 103 | + verificationStatus: TEST_ACCEPTED |
| 104 | + verifiedWith: [] |
| 105 | +documentation_of: test/primality_test.test.cpp |
| 106 | +layout: document |
| 107 | +redirect_from: |
| 108 | +- /verify/test/primality_test.test.cpp |
| 109 | +- /verify/test/primality_test.test.cpp.html |
| 110 | +title: test/primality_test.test.cpp |
| 111 | +--- |
0 commit comments