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
9bool PrimalityTest(ll n) {
10 if(n==2) return true;
11 if(n<=1 || n%2==0) return false;
12
13 vector<ll> test;
14 if(n<4759123141ll) test={2,7,61};
15 else test={2,325,9375,28178,450775,9780504,1795265022};
16
17 ll s=0,d=n-1;
18 while(d%2==0) d>>=1,s++;
19
20 for(ll a: test) {
21 if(a>=n) break;
22 lll x=ModPow<lll>(a,d,n);
23
24 if(x==1||x==n-1) continue;
25 else {
26 for(ll r=1; r<s; r++) {
27 x=x*x%n;
28 if(x==1) return false;
29 else if(x==n-1) break;
30 }
31 }
32 if(x!=n-1) return false;
33 }
34
35 return true;
36}
bool PrimalityTest(ll n)
ミラーラビン素数判定法により n が素数であるかを判定する