Skip to content

Commit 187c1b7

Browse files
committed
support base hashing in primality testing, typedef big_vector
1 parent 53a2452 commit 187c1b7

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

cp-algo/number_theory/primality.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ namespace cp_algo::math {
2626
}
2727
return x == -1;
2828
};
29-
return base::with_mod(m, [&](){
30-
// Works for all m < 2^64: https://miller-rabin.appspot.com
31-
return std::ranges::all_of(std::array{
32-
2, 325, 9375, 28178, 450775, 9780504, 1795265022
33-
}, test);
29+
return base::with_mod(m, [&]() {
30+
#ifdef CP_ALGO_NUMBER_THEORY_PRIMALITY_BASES_HPP
31+
uint16_t base2 = 7, base3 = 61;
32+
if (m != uint32_t(m)) {
33+
base2 = base_table1[uint32_t(m * 0xAD625B89) >> 18];
34+
base3 = base_table2[base2 >> 13];
35+
}
36+
return test(2) && test(base2) && test(base3);
37+
#else
38+
return std::ranges::all_of(std::array{2, 325, 9375, 28178, 450775, 9780504, 1795265022}, test);
39+
#endif
3440
});
3541
}
3642
}

cp-algo/number_theory/primality_bases.hpp

Lines changed: 7 additions & 0 deletions
Large diffs are not rendered by default.

cp-algo/util/big_alloc.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CP_ALGO_UTIL_big_alloc_HPP
22
#define CP_ALGO_UTIL_big_alloc_HPP
33

4+
#include <vector>
45
#include <cstddef>
56
#include <iostream>
67

@@ -59,5 +60,8 @@ namespace cp_algo {
5960
return (x + Align - 1) / Align * Align;
6061
}
6162
};
63+
64+
template<typename T>
65+
using big_vector = std::vector<T, big_alloc<T>>;
6266
}
6367
#endif // CP_ALGO_UTIL_big_alloc_HPP

verify/number_theory/primality.test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// @brief Primality Test
22
#define PROBLEM "https://judge.yosupo.jp/problem/primality_test"
33
#pragma GCC optimize("Ofast,unroll-loops")
4+
#include <iostream>
5+
#include "blazingio/blazingio.min.hpp"
6+
#include "cp-algo/number_theory/primality_bases.hpp"
47
#include "cp-algo/number_theory/primality.hpp"
58
#include <bits/stdc++.h>
69

0 commit comments

Comments
 (0)