Kyopro Library
読み取り中…
検索中…
一致する文字列を見つけられません
primality_test.hpp
[詳解]
1
#
pragma
once
2
#
include
"../../kyopro_library/template.hpp"
3
#
include
"../../kyopro_library/others/modcal.hpp"
4
5
/// @brief ミラーラビン素数判定法により n が素数であるかを判定する
6
/// @note O(k log^3 n)
7
/// @ref https://drken1215.hatenablog.com/entry/2023/05/23/233000
8
/// @ref verify: https://judge.yosupo.jp/problem/primality_test
9
bool
PrimalityTest
(ll n) {
10
using
lll=__int128_t;
11
if
(n==2)
return
true
;
12
if
(n<=1 || n%2==0)
return
false
;
13
14
vector<ll> test;
15
if
(n<4759123141ll) test={2,7,61};
16
else
test={2,325,9375,28178,450775,9780504,1795265022};
17
18
ll s=0, d=n-1;
19
while
(d%2==0) d>>=1, s++;
20
21
for
(ll a: test) {
22
if
(a>=n)
break
;
23
lll x=ModPow<lll>(a,d,n);
24
25
if
(x==1 || x==n-1)
continue
;
26
else
{
27
for
(ll r=1; r<s; r++) {
28
x=x*x%n;
29
if
(x==1)
return
false
;
30
else
if
(x==n-1)
break
;
31
}
32
}
33
if
(x!=n-1)
return
false
;
34
}
35
36
return
true
;
37
}
PrimalityTest
bool PrimalityTest(ll n)
ミラーラビン素数判定法により n が素数であるかを判定する
Definition
primality_test.hpp:9
math
primality_test.hpp
構築:
1.13.2